Skip to content

Commit e170733

Browse files
committed
Week 11 Solutions
1 parent 923919b commit e170733

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
/*
16+
Time complexity: O(N)
17+
Space complexity: O(N) h: 트리의 높이
18+
*/
19+
function maxPathSum(root: TreeNode | null): number {
20+
let maxSum = -Infinity
21+
const dfs = (node: TreeNode): number => {
22+
if (node == null) return 0
23+
24+
const leftMax = Math.max(0, dfs(node.left))
25+
const rightMax = Math.max(0, dfs(node.right))
26+
const currentMax = node.val + leftMax + rightMax
27+
28+
maxSum = Math.max(maxSum, currentMax)
29+
return node.val + Math.max(leftMax, rightMax)
30+
}
31+
dfs(root)
32+
return maxSum
33+
};

merge-intervals/hoyeongkwak.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*
2+
Time complexity: O(NlogN)
3+
Space complexity: O(N)
4+
*/
5+
function merge(intervals: number[][]): number[][] {
6+
if (intervals.length === 0) return intervals
7+
intervals.sort((a, b) => a[0] - b[0])
8+
const result = []
9+
for (const interval of intervals) {
10+
if (result.length === 0 || result[result.length - 1][1] < interval[0]) {
11+
result.push(interval)
12+
} else {
13+
result[result.length - 1][1] = Math.max(result[result.length -1][1], interval[1])
14+
}
15+
}
16+
return result
17+
};

missing-number/hoyeongkwak.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
Time complexity: O(N)
3+
Space complexity: O(N)
4+
*/
5+
function missingNumber(nums: number[]): number {
6+
const numSet = new Set<number>(nums)
7+
let missNum = 0
8+
for (let i = 0; i <= nums.length; i++) {
9+
if (!numSet.has(i)) {
10+
missNum = i
11+
break
12+
}
13+
}
14+
return missNum
15+
};

reorder-list/hoyeongkwak.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
/**
14+
Do not return anything, modify head in-place instead.
15+
*/
16+
/*
17+
Time complexity: O(N)
18+
Space complexity: O(1)
19+
*/
20+
function reorderList(head: ListNode | null): void {
21+
let slow = head
22+
let fast = head
23+
while (fast && fast.next) {
24+
slow = slow.next
25+
fast = fast.next.next
26+
}
27+
let curr = slow.next
28+
slow.next = null
29+
let prev = null
30+
31+
while (curr) {
32+
let temp = curr.next
33+
curr.next = prev
34+
prev = curr
35+
curr = temp
36+
}
37+
let first = head
38+
let second = prev
39+
40+
while (second) {
41+
let firstNext = first.next
42+
let secondNext = second.next
43+
first.next = second
44+
second.next = firstNext
45+
first = firstNext
46+
second = secondNext
47+
}
48+
};

0 commit comments

Comments
 (0)