Skip to content

Commit bb13635

Browse files
authored
Merge pull request #1572 from HoonDongKang/main
[HoonDongKang] Week 11 Solutions
2 parents e6bf845 + bfe0319 commit bb13635

File tree

5 files changed

+271
-0
lines changed

5 files changed

+271
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* [Problem]: [124 Binary Tree Maximum Path Sum
3+
* (https://leetcode.com/problems/binary-tree-maximum-path-sum/description/)
4+
*/
5+
/**
6+
* Definition for a binary tree node.
7+
* class TreeNode {
8+
* val: number
9+
* left: TreeNode | null
10+
* right: TreeNode | null
11+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
12+
* this.val = (val===undefined ? 0 : val)
13+
* this.left = (left===undefined ? null : left)
14+
* this.right = (right===undefined ? null : right)
15+
* }
16+
* }
17+
*/
18+
19+
//시간복잡도 O(n)
20+
//공간복잡도 O(h)
21+
function maxPathSum(root: TreeNode | null): number {
22+
if (!root) return 0;
23+
let result = root.val;
24+
25+
function dfs(node: TreeNode | null) {
26+
if (!node) return 0;
27+
28+
let leftMax = Math.max(dfs(node.left), 0);
29+
let rightMax = Math.max(dfs(node.right), 0);
30+
31+
result = Math.max(node.val + leftMax + rightMax, result);
32+
33+
return node.val + Math.max(leftMax, rightMax);
34+
}
35+
36+
dfs(root);
37+
return result;
38+
}

graph-valid-tree/HoonDongKang.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* [Problem]: [178] Graph Valid Tree
3+
* (https://www.lintcode.com/problem/178/)
4+
*/
5+
6+
export class Solution {
7+
/**
8+
* @param n: An integer
9+
* @param edges: a list of undirected edges
10+
* @return: true if it's a valid tree, or false
11+
*/
12+
//시간복잡도 O(n+e)
13+
//공간복잡도 O(n+e)
14+
validTree(n: number, edges: number[][]): boolean {
15+
const graph: number[][] = Array.from({ length: n }, () => []);
16+
for (const [a, b] of edges) {
17+
graph[a].push(b);
18+
graph[b].push(a);
19+
}
20+
21+
const visited = new Set<number>();
22+
23+
function hasCycle(node: number, prev: number): boolean {
24+
if (visited.has(node)) return true;
25+
visited.add(node);
26+
27+
for (const neighbor of graph[node]) {
28+
if (neighbor === prev) continue;
29+
if (hasCycle(neighbor, node)) return true;
30+
}
31+
return false;
32+
}
33+
34+
if (hasCycle(0, -1)) return false;
35+
36+
return visited.size === n;
37+
}
38+
39+
//시간복잡도 O(n)
40+
//공간복잡도 O(n)
41+
validTree2(n: number, edges: number[][]): boolean {
42+
if (edges.length !== n - 1) return false;
43+
44+
const graph: number[][] = Array.from({ length: n }, () => []);
45+
for (const [a, b] of edges) {
46+
graph[a].push(b);
47+
graph[b].push(a);
48+
}
49+
50+
const visited = new Set<number>();
51+
52+
function dfs(node: number) {
53+
visited.add(node);
54+
for (const neighbor of graph[node]) {
55+
if (!visited.has(neighbor)) {
56+
dfs(neighbor);
57+
}
58+
}
59+
}
60+
61+
dfs(0);
62+
63+
return visited.size === n;
64+
}
65+
}

merge-intervals/HoonDongKang.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* [Problem]: [56] Merge Intervals
3+
* (https://leetcode.com/problems/merge-intervals/description/)
4+
*/
5+
function merge(intervals: number[][]): number[][] {
6+
//시간복잡도 O(n^2)
7+
//공간복잡도 O(n)
8+
function bruteForceFunc(intervals: number[][]): number[][] {
9+
const result: number[][] = [];
10+
const visited = new Set<number>();
11+
12+
for (let i = 0; i < intervals.length; i++) {
13+
if (visited.has(i)) continue;
14+
visited.add(i);
15+
16+
while (true) {
17+
let hasMerged = false;
18+
for (let j = 0; j < intervals.length; j++) {
19+
if (visited.has(j)) continue;
20+
21+
const [curA, curB] = intervals[i];
22+
const [nextA, nextB] = intervals[j];
23+
24+
if (curA <= nextB && nextA <= curB) {
25+
visited.add(j);
26+
intervals[i] = [Math.min(curA, nextA), Math.max(curB, nextB)];
27+
28+
hasMerged = true;
29+
}
30+
}
31+
if (!hasMerged) break;
32+
}
33+
result.push(intervals[i]);
34+
}
35+
return result;
36+
}
37+
38+
//시간복잡도 O(nlog n)
39+
//공간복잡도 O(n)
40+
function sortFunc(intervals: number[][]): number[][] {
41+
if (!intervals.length) return intervals;
42+
intervals.sort((a, b) => a[0] - b[0]);
43+
44+
const result: number[][] = [intervals[0]];
45+
for (let i = 0; i < intervals.length; i++) {
46+
const prev = result[result.length - 1];
47+
const cur = intervals[i];
48+
49+
if (cur[0] <= prev[1]) {
50+
prev[1] = Math.max(prev[1], cur[1]);
51+
} else {
52+
result.push(cur);
53+
}
54+
}
55+
56+
return result;
57+
}
58+
}

missing-number/HoonDongKang.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* [Problem]: [268] Missing Number
3+
* (https://leetcode.com/problems/missing-number/description/)
4+
*/
5+
function missingNumber(nums: number[]): number {
6+
//시간복잡도 O(n^2)
7+
//공간복잡도 O(1)
8+
function loopFunc(nums: number[]): number {
9+
let num = 0;
10+
while (true) {
11+
if (!nums.includes(num)) break;
12+
num++;
13+
}
14+
15+
return num;
16+
}
17+
//시간복잡도 O(n)
18+
//공간복잡도 O(1)
19+
function sumFunc(nums: number[]): number {
20+
const n = nums.length;
21+
const sum = nums.reduce((acc, cur) => (acc += cur), 0);
22+
const expected = (n * (n + 1)) / 2;
23+
24+
return expected - sum;
25+
}
26+
}

reorder-list/HoonDongKang.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/**
2+
* [Problem]: [143] Reorder List
3+
* (https://leetcode.com/problems/reorder-list/)
4+
*/
5+
6+
class ListNode {
7+
val: number;
8+
next: ListNode | null;
9+
constructor(val?: number, next?: ListNode | null) {
10+
this.val = val === undefined ? 0 : val;
11+
this.next = next === undefined ? null : next;
12+
}
13+
}
14+
15+
/**
16+
Do not return anything, modify head in-place instead.
17+
*/
18+
function reorderList(head: ListNode | null): void {
19+
//시간복잡도 O(n)
20+
//공간복잡도 O(n)
21+
function stackFunc(head: ListNode | null): void {
22+
if (!head || !head.next) return;
23+
24+
const stack: ListNode[] = [];
25+
let node: ListNode | null = head;
26+
27+
while (node) {
28+
stack.push(node);
29+
node = node.next;
30+
}
31+
32+
const length = stack.length;
33+
node = head;
34+
35+
for (let i = 0; i < Math.floor(length / 2); i++) {
36+
const tail = stack.pop()!;
37+
const next: ListNode | null = node.next;
38+
39+
node.next = tail;
40+
tail.next = next;
41+
42+
node = next!;
43+
}
44+
45+
node.next = null;
46+
}
47+
//시간복잡도 O(n)
48+
//공간복잡도 O(1)
49+
function twoPointerFunc(head: ListNode | null): void {
50+
if (!head || !head.next) return;
51+
52+
let slow = head;
53+
let fast = head;
54+
while (fast && fast.next && fast.next.next) {
55+
slow = slow.next!;
56+
fast = fast.next.next;
57+
}
58+
59+
let prev: ListNode | null = null;
60+
let curr = slow.next;
61+
slow.next = null;
62+
63+
while (curr) {
64+
const next = curr.next;
65+
curr.next = prev;
66+
prev = curr;
67+
curr = next;
68+
}
69+
70+
let first = head;
71+
let second = prev;
72+
73+
while (second) {
74+
const tmp1 = first!.next;
75+
const tmp2 = second.next;
76+
77+
first!.next = second;
78+
second.next = tmp1;
79+
80+
first = tmp1!;
81+
second = tmp2;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)