Skip to content

Commit 6b13bd1

Browse files
authored
Merge pull request #1589 from Jeehay28/main
[Jeehay28] WEEK 12 Solutions
2 parents c91a559 + b70bf18 commit 6b13bd1

File tree

4 files changed

+199
-0
lines changed

4 files changed

+199
-0
lines changed

non-overlapping-intervals/Jeehay28.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// TC: O(n log n)
2+
// SC: O(1)
3+
function eraseOverlapIntervals(intervals: number[][]): number {
4+
intervals.sort((a, b) => a[1] - b[1]);
5+
// [1, 2], [2, 3], [1, 3], [3, 4]
6+
7+
let prevEnd = intervals[0][1];
8+
let removalCount = 0;
9+
10+
for (let i = 1; i < intervals.length; i++) {
11+
if (prevEnd > intervals[i][0]) {
12+
removalCount++;
13+
// skip current interval
14+
} else {
15+
prevEnd = intervals[i][1];
16+
}
17+
}
18+
19+
return removalCount;
20+
}
21+
22+
23+
// TC: O(n log n)
24+
// SC: O(1)
25+
// function eraseOverlapIntervals(intervals: number[][]): number {
26+
// intervals.sort((a, b) => a[0] - b[0]);
27+
28+
// let prev = intervals[0];
29+
// let cnt = 1;
30+
// let removalCount = 0;
31+
32+
// while (cnt < intervals.length) {
33+
// const current = intervals[cnt];
34+
35+
// if (prev[1] <= current[0]) {
36+
// prev = current;
37+
// } else {
38+
// prev = [Math.min(prev[0], current[0]), Math.min(prev[1], current[1])];
39+
// removalCount++;
40+
// }
41+
// cnt++;
42+
// }
43+
44+
// return removalCount;
45+
// }
46+
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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+
// TC: O(n)
11+
// SC: O(1)
12+
function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
13+
let dummy = new ListNode();
14+
dummy.next = head;
15+
let fast: ListNode | null = dummy;
16+
let slow: ListNode | null = dummy;
17+
18+
for (let i = 0; i < n + 1; i++) {
19+
if (fast) {
20+
fast = fast.next;
21+
}
22+
}
23+
24+
while (fast) {
25+
fast = fast.next;
26+
slow = slow!.next;
27+
}
28+
29+
if (slow && slow.next) {
30+
slow.next = slow.next.next;
31+
}
32+
33+
return dummy.next;
34+
}
35+
36+
37+
// TC: O(n)
38+
// SC: O(n)
39+
// function removeNthFromEnd(head: ListNode | null, n: number): ListNode | null {
40+
// let dummy = head;
41+
// const nodes: (ListNode | null)[] = [];
42+
43+
// while (dummy) {
44+
// nodes.push(dummy);
45+
// dummy = dummy.next;
46+
// }
47+
48+
// // first node: nodes.length === n
49+
// // last node: n === 1
50+
51+
// if (nodes.length === n) {
52+
// head = head!.next;
53+
// } else if (nodes.length > 1 && n === 1) {
54+
// nodes[nodes.length - 2]!.next = null;
55+
// } else {
56+
// nodes[nodes.length - n - 1]!.next = nodes[nodes.length - n + 1];
57+
// }
58+
59+
// return head;
60+
// }

same-tree/Jeehay28.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+
// TC: O(n)
13+
// SC: O(n)
14+
function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
15+
if (!p && !q) return true;
16+
if (!p || !q) return false;
17+
18+
if (p.val !== q.val) return false;
19+
20+
return isSameTree(p.left, q.left) && isSameTree(p.right, q.right);
21+
}
22+
23+
24+
// TC: O(n)
25+
// SC: O(n)
26+
// function isSameTree(p: TreeNode | null, q: TreeNode | null): boolean {
27+
// const stack: (TreeNode | null)[][] = [[p, q]];
28+
29+
// while (stack.length > 0) {
30+
// const [node1, node2] = stack.pop();
31+
32+
// if (!node1 && !node2) continue;
33+
// if (!node1 || !node2) return false;
34+
// if (node1.val !== node2.val) return false;
35+
36+
// stack.push([node1.left, node2.left]);
37+
// stack.push([node1.right, node2.right]);
38+
// }
39+
40+
// return true;
41+
// }
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
* Encodes a tree to a single string.
14+
*/
15+
16+
// TC: O(n)
17+
// SC: O(n)
18+
function serialize(root: TreeNode | null): string {
19+
if (!root) return "null";
20+
return `${root.val},${serialize(root.left)},${serialize(root.right)}`;
21+
}
22+
23+
/*
24+
* Decodes your encoded data to tree.
25+
*/
26+
27+
// TC: O(n)
28+
// SC: O(n)
29+
function deserialize(data: string): TreeNode | null {
30+
const values = data.split(",");
31+
let idx = 0;
32+
33+
const dfs = () => {
34+
if (values[idx] === "null") {
35+
idx++;
36+
return null;
37+
}
38+
39+
const node = new TreeNode(parseInt(values[idx]));
40+
idx++;
41+
node.left = dfs();
42+
node.right = dfs();
43+
return node;
44+
};
45+
46+
return dfs();
47+
}
48+
49+
/**
50+
* Your functions will be called as such:
51+
* deserialize(serialize(root));
52+
*/

0 commit comments

Comments
 (0)