Skip to content

Commit 720b898

Browse files
authored
Merge pull request #2489 from soobing/week4
[soobing] WEEK 04 Solutions
2 parents 60f8363 + 0207044 commit 720b898

File tree

5 files changed

+99
-0
lines changed

5 files changed

+99
-0
lines changed

coin-change/soobing3.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function coinChange(coins: number[], amount: number): number {
2+
const dp = new Array(amount + 1).fill(Infinity);
3+
dp[0] = 0;
4+
5+
for (let i = 0; i <= amount; i++) {
6+
for(const coin of coins) {
7+
if (i - coin >= 0 && dp[i - coin] !== Infinity) {
8+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
9+
}
10+
}
11+
}
12+
return dp[amount] === Infinity ? -1 : dp[amount];
13+
};
14+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
function findMin(nums: number[]): number {
2+
let left = 0;
3+
let right = nums.length - 1;
4+
while(left < right) {
5+
const mid = Math.floor((left + right) / 2);
6+
if(nums[mid] > nums[right]) {
7+
left = mid + 1;
8+
} else {
9+
right = mid;
10+
}
11+
}
12+
return nums[left];
13+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
class TreeNode {
3+
val: number
4+
left: TreeNode | null
5+
right: TreeNode | null
6+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
7+
this.val = (val===undefined ? 0 : val)
8+
this.left = (left===undefined ? null : left)
9+
this.right = (right===undefined ? null : right)
10+
}
11+
}
12+
13+
function maxDepth(root: TreeNode | null): number {
14+
if(!root) return 0;
15+
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right))
16+
};

merge-two-sorted-lists/soobing3.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
2+
class ListNode {
3+
val: number
4+
next: ListNode | null
5+
constructor(val?: number, next?: ListNode | null) {
6+
this.val = (val===undefined ? 0 : val)
7+
this.next = (next===undefined ? null : next)
8+
}
9+
}
10+
11+
12+
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
13+
let mergedList = new ListNode(0, null);
14+
const result = mergedList;
15+
16+
while(list1 && list2) {
17+
if(list1.val < list2.val) {
18+
mergedList.next = list1;
19+
list1 = list1.next;
20+
} else {
21+
mergedList.next = list2;
22+
list2 = list2.next;
23+
}
24+
mergedList = mergedList.next;
25+
}
26+
27+
mergedList.next = list1 ?? list2;
28+
29+
return result.next;
30+
};

word-search/soobing3.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function exist(board: string[][], word: string): boolean {
2+
const move = [[1, 0], [-1, 0], [0, 1], [0, -1]];
3+
4+
function dfs(row: number, col: number, index: number) {
5+
if(board[row][col] !== word[index]) return false;
6+
7+
const temp = board[row][col];
8+
board[row][col] = '#';
9+
10+
if(index === word.length - 1) return true;
11+
for(const [dx, dy] of move) {
12+
if(row + dx < 0 || row + dx >= board.length) continue;
13+
if(col + dy < 0 || col + dy >= board[0].length) continue;
14+
if(dfs(row + dx, col + dy, index + 1)) return true;
15+
}
16+
17+
board[row][col] = temp;
18+
}
19+
20+
for (let r = 0; r < board.length; r++) {
21+
for (let c = 0; c < board[0].length; c++) {
22+
if (dfs(r, c, 0)) return true;
23+
}
24+
}
25+
return false;
26+
};

0 commit comments

Comments
 (0)