Skip to content

Commit 35eb79f

Browse files
authored
Merge pull request #2466 from junzero741/main
[junzero741] WEEK 04 Solutions
2 parents f801651 + abea895 commit 35eb79f

4 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// TC: O(logN)
2+
// SC: O(1)
3+
function findMin(nums: 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+
if(nums[mid] > nums[right]) {
10+
left = mid + 1;
11+
} else {
12+
right = mid;
13+
}
14+
}
15+
16+
return nums[left];
17+
18+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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 maxDepth(root: TreeNode | null): number {
15+
if(!root) {
16+
return 0;
17+
}
18+
19+
let maxLv = 1;
20+
21+
function dfs(node: TreeNode, lv: number) {
22+
if(node.left) {
23+
dfs(node.left, lv+1)
24+
}
25+
if(node.right) {
26+
dfs(node.right, lv+1)
27+
}
28+
maxLv = Math.max(maxLv, lv)
29+
}
30+
31+
32+
dfs(root, 1);
33+
34+
35+
return maxLv;
36+
37+
38+
};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
12+
13+
const dummy = new ListNode();
14+
let tail = dummy;
15+
16+
while (list1 && list2) {
17+
if (list1.val <= list2.val) {
18+
tail.next = list1;
19+
list1 = list1.next;
20+
} else {
21+
tail.next = list2;
22+
list2 = list2.next;
23+
}
24+
tail = tail.next;
25+
}
26+
27+
tail.next = list1 || list2;
28+
29+
return dummy.next;
30+
};

word-search/junzero741.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// TC: O(M*N*4^L)
2+
// SC: O(L)
3+
function exist(board: string[][], word: string): boolean {
4+
const rows = board.length;
5+
const cols = board[0].length;
6+
7+
function dfs(r: number, c: number, index: number): boolean {
8+
if (index === word.length) return true;
9+
10+
if (
11+
r < 0 || r >= rows ||
12+
c < 0 || c >= cols ||
13+
board[r][c] !== word[index]
14+
) {
15+
return false;
16+
}
17+
18+
const temp = board[r][c];
19+
board[r][c] = '#';
20+
21+
const found =
22+
dfs(r + 1, c, index + 1) ||
23+
dfs(r - 1, c, index + 1) ||
24+
dfs(r, c + 1, index + 1) ||
25+
dfs(r, c - 1, index + 1);
26+
27+
board[r][c] = temp;
28+
29+
return found;
30+
}
31+
32+
for (let i = 0; i < rows; i++) {
33+
for (let j = 0; j < cols; j++) {
34+
if (board[i][j] === word[0] && dfs(i, j, 0)) {
35+
return true;
36+
}
37+
}
38+
}
39+
40+
return false;
41+
}

0 commit comments

Comments
 (0)