Skip to content

Commit 84c31e0

Browse files
authored
Merge pull request #563 from HC-kang/main
[강희찬] WEEK 12 Solution
2 parents ea008d3 + 221da7b commit 84c31e0

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

merge-intervals/HC-kang.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* https://leetcode.com/problems/merge-intervals
3+
* T.C. O(n logn)
4+
* S.C. O(n)
5+
*/
6+
function merge(intervals: number[][]): number[][] {
7+
intervals.sort((a, b) => a[0] - b[0]); // T.C. O(n logn)
8+
9+
const result = [intervals[0]]; // S.C. O(n)
10+
11+
// T.C. O(n)
12+
for (let i = 1; i < intervals.length; i++) {
13+
const last = result[result.length - 1];
14+
const current = intervals[i];
15+
16+
if (last[1] >= current[0]) {
17+
last[1] = Math.max(last[1], current[1]);
18+
} else {
19+
result.push(current);
20+
}
21+
}
22+
23+
return result;
24+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/**
2+
* https://leetcode.com/problems/number-of-connected-components-in-an-undirected-graph
3+
* T.C. O(V + E)
4+
* S.C. O(V + E)
5+
*/
6+
function countComponents(n: number, edges: number[][]): number {
7+
const graph = new Map<number, number[]>();
8+
9+
for (let i = 0; i < n; i++) {
10+
graph.set(i, []);
11+
}
12+
13+
for (const [u, v] of edges) {
14+
graph.get(u)!.push(v);
15+
graph.get(v)!.push(u);
16+
}
17+
18+
function dfs(node: number) {
19+
visited.add(node);
20+
21+
for (const neighbor of graph.get(node)!) {
22+
if (!visited.has(neighbor)) {
23+
dfs(neighbor);
24+
}
25+
}
26+
}
27+
28+
const visited = new Set<number>();
29+
30+
let components = 0;
31+
32+
for (let i = 0; i < n; i++) {
33+
if (!visited.has(i)) {
34+
components++;
35+
dfs(i);
36+
}
37+
}
38+
39+
return components;
40+
}
41+
42+
/**
43+
* Using Union Find
44+
* T.C. O(V + E)
45+
*
46+
*/
47+
function countComponents(n: number, edges: number[][]): number {
48+
const parent = Array.from({ length: n }, (_, i) => i);
49+
50+
// find and compress path
51+
function find(x: number): number {
52+
if (parent[x] !== x) {
53+
parent[x] = find(parent[x]);
54+
}
55+
return parent[x];
56+
}
57+
58+
// union two sets and check if they have the same root
59+
function union(x: number, y: number): boolean {
60+
const rootX = find(x);
61+
const rootY = find(y);
62+
parent[rootX] = rootY;
63+
return true;
64+
}
65+
66+
for (const [x, y] of edges) {
67+
union(x, y);
68+
}
69+
70+
return new Set(parent.map(find)).size;
71+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
/**
11+
* https://leetcode.com/problems/remove-nth-node-from-end-of-list
12+
* T.C. O(N)
13+
* S.C. O(1)
14+
*/
15+
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
16+
let first = head;
17+
let second = head;
18+
19+
for (let i = 0; i < n; i++)
20+
first = first!.next;
21+
22+
if (!first)
23+
return head!.next;
24+
25+
while (first.next) {
26+
first = first.next;
27+
second = second!.next;
28+
}
29+
30+
second!.next = second!.next!.next;
31+
32+
return head;
33+
}

same-tree/HC-kang.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
/**
13+
* https://leetcode.com/problems/same-tree/description/
14+
* T.C. O(n)
15+
* S.C. O(n)
16+
*/
17+
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
18+
if (p === null && q === null) return true;
19+
if (p === null || q === null) return false;
20+
if (p.val !== q.val) return false;
21+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
22+
}
23+
24+
/**
25+
* T.C. O(n)
26+
* S.C. O(n)
27+
*/
28+
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
29+
const stack: [TreeNode | null, TreeNode | null][] = [[p, q]];
30+
31+
while (stack.length) {
32+
const [node1, node2] = stack.pop()!;
33+
if (node1 === null && node2 === null) continue;
34+
if (node1 === null || node2 === null) return false;
35+
if (node1.val !== node2.val) return false;
36+
stack.push([node1.left, node2.left]);
37+
stack.push([node1.right, node2.right]);
38+
}
39+
40+
return true;
41+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
/**
13+
* https://leetcode.com/problems/serialize-and-deserialize-binary-tree
14+
* T.C. O(n)
15+
* S.C. O(n)
16+
*/
17+
function serialize(root: TreeNode | null): string {
18+
return JSON.stringify(root);
19+
}
20+
21+
function deserialize(data: string): TreeNode | null {
22+
return JSON.parse(data);
23+
}
24+
25+
/**
26+
* Recursive
27+
* T.C. O(n)
28+
* S.C. O(n)
29+
*/
30+
function serialize(root: TreeNode | null): string {
31+
return root
32+
? `${root.val},${serialize(root.left)},${serialize(root.right)}`
33+
: 'null';
34+
}
35+
36+
function deserialize(data: string): TreeNode | null {
37+
const values = data.split(',');
38+
let index = 0;
39+
40+
function dfs(): TreeNode | null {
41+
const val = values[index++];
42+
if (val === 'null') return null;
43+
44+
const node = new TreeNode(parseInt(val));
45+
node.left = dfs();
46+
node.right = dfs();
47+
return node;
48+
}
49+
50+
return dfs();
51+
}

0 commit comments

Comments
 (0)