Skip to content

Commit c05ddb4

Browse files
authored
Merge pull request #1543 from Jeehay28/main
[Jeehay28] Week 10 Solutions
2 parents de7ae70 + b6d43ef commit c05ddb4

File tree

5 files changed

+254
-0
lines changed

5 files changed

+254
-0
lines changed

course-schedule/Jeehay28.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// TC: O(V + E), V = numCourses, E = prerequisites.length
2+
// SC: O(V + E)
3+
4+
function canFinish(numCourses: number, prerequisites: number[][]): boolean {
5+
// 0: [1]
6+
// 1: []
7+
8+
const graph = new Map<number, number[]>();
9+
10+
// Initialize all courses
11+
for (let i = 0; i < numCourses; i++) {
12+
graph.set(i, []);
13+
}
14+
15+
// Build the graph
16+
for (const [crs, pre] of prerequisites) {
17+
graph.get(crs)!.push(pre);
18+
}
19+
20+
const traversing = new Set<number>();
21+
const finished = new Set<number>();
22+
23+
const dfs = (crs: number): boolean => {
24+
if (traversing.has(crs)) return false; // cycle detected
25+
26+
if (finished.has(crs)) return true; // already visited
27+
28+
traversing.add(crs);
29+
30+
for (const pre of graph.get(crs)!) {
31+
if (!dfs(pre)) return false;
32+
}
33+
34+
traversing.delete(crs);
35+
finished.add(crs);
36+
37+
return true;
38+
};
39+
40+
for (const crs of graph.keys()) {
41+
if (!dfs(crs)) return false;
42+
}
43+
44+
return true;
45+
}
46+

invert-binary-tree/Jeehay28.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 invertTree(root: TreeNode | null): TreeNode | null {
15+
if (!root) return null;
16+
17+
const left = invertTree(root.right);
18+
const right = invertTree(root.left);
19+
20+
root.left = left;
21+
root.right = right;
22+
23+
return root;
24+
}
25+
26+
27+
// TC: O(n)
28+
// SC: O(n)
29+
// function invertTree(root: TreeNode | null): TreeNode | null {
30+
// if (!root) return null;
31+
32+
// [root.left, root.right] = [root.right, root.left];
33+
34+
// invertTree(root.left);
35+
// invertTree(root.right);
36+
37+
// return root;
38+
39+
// }
40+
41+
42+
// TC: O(n)
43+
// SC: O(n)
44+
// function invertTree(root: TreeNode | null): TreeNode | null {
45+
// if (!root) return null;
46+
47+
// const stack: (TreeNode | null)[] = [root];
48+
49+
// while (stack.length > 0) {
50+
// const node = stack.pop();
51+
52+
// if (!node) continue;
53+
54+
// [node.left, node.right] = [node.right, node.left];
55+
56+
// stack.push(node.left);
57+
// stack.push(node.right);
58+
// }
59+
60+
// return root;
61+
// }
62+

jump-game/Jeehay28.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
function canJump(nums: number[]): boolean {
4+
// nums = [3, 2, 1, 0, 4]
5+
// farthest 3, 3, 3, 3
6+
let farthest = 0;
7+
8+
for (let i = 0; i < nums.length; i++) {
9+
if (i > farthest) return false;
10+
farthest = Math.max(farthest, nums[i] + i);
11+
}
12+
return true;
13+
}
14+
15+
16+
// ❌ TC: O(n^2)
17+
// SC: O(n)
18+
19+
// function canJump(nums: number[]): boolean {
20+
// const lastIndex = nums.length - 1;
21+
// const visited = new Map<number, boolean>();
22+
23+
// const canReach = (current: number): boolean => {
24+
// if (current === lastIndex) return true;
25+
26+
// if (visited.has(current)) return visited.get(current) as boolean;
27+
28+
// for (let i = 1; i <= nums[current]; i++) {
29+
// if (current + i <= lastIndex) {
30+
// if (canReach(current + i)) return true;
31+
// }
32+
// }
33+
34+
// visited.set(current, false);
35+
36+
// return false;
37+
// };
38+
39+
// return canReach(0);
40+
// }

merge-k-sorted-lists/Jeehay28.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 * log n), n = total number of all nodes combined (across all lists)
11+
// SC: O(n)
12+
// TDDO: Implement more optimized solution using Min Heap with TC: O(n log k)
13+
function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
14+
const nodeValues: number[] = [];
15+
16+
for (let node of lists) {
17+
while (node) {
18+
nodeValues.push(node.val);
19+
node = node.next;
20+
}
21+
}
22+
23+
nodeValues.sort((a, b) => a - b);
24+
25+
if (nodeValues.length === 0) return null;
26+
27+
const head = new ListNode(nodeValues[0]);
28+
let temp = head;
29+
30+
for (let i = 1; i < nodeValues.length; i++) {
31+
temp.next = new ListNode(nodeValues[i]);
32+
temp = temp.next;
33+
}
34+
35+
return head;
36+
}
37+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// TC: O(log n)
2+
// SC: O(1)
3+
function search(nums: number[], target: number): number {
4+
let left = 0;
5+
let right = nums.length - 1;
6+
7+
while (left <= right) {
8+
const mid = Math.floor((left + right) / 2);
9+
10+
if (nums[mid] === target) return mid;
11+
12+
if (nums[left] <= nums[mid]) {
13+
// Left half is sorted
14+
if (nums[left] <= target && target < nums[mid]) {
15+
right = mid - 1;
16+
} else {
17+
left = mid + 1;
18+
}
19+
} else {
20+
// Right half is sorted
21+
if (nums[mid] < target && target <= nums[right]) {
22+
left = mid + 1;
23+
} else {
24+
right = mid - 1;
25+
}
26+
}
27+
}
28+
29+
return -1;
30+
}
31+
32+
// TC: O(log n)
33+
// SC: O(1)
34+
// function search(nums: number[], target: number): number {
35+
// const findPivot = (nums: number[]) => {
36+
// let left = 0;
37+
// let right = nums.length - 1;
38+
39+
// while (left <= right) {
40+
// const mid = Math.floor((left + right) / 2);
41+
// if (mid > 0 && nums[mid - 1] > nums[mid]) return mid;
42+
// if (nums[0] <= nums[mid]) {
43+
// left = mid + 1;
44+
// } else {
45+
// right = mid - 1;
46+
// }
47+
// }
48+
// return 0;
49+
// };
50+
51+
// const binarySearch = (left: number, right: number) => {
52+
// while (left <= right) {
53+
// const mid = Math.floor((left + right) / 2);
54+
// if (nums[mid] === target) return mid;
55+
// if (nums[mid] < target) {
56+
// left = mid + 1;
57+
// } else {
58+
// right = mid - 1;
59+
// }
60+
// }
61+
// return -1;
62+
// };
63+
64+
// const pivot = findPivot(nums);
65+
// return Math.max(
66+
// binarySearch(0, pivot - 1),
67+
// binarySearch(pivot, nums.length - 1)
68+
// );
69+
// }

0 commit comments

Comments
 (0)