Skip to content

Commit be882e1

Browse files
committed
WEEK 4 Solutions
1 parent 8a1541d commit be882e1

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

coin-change/hoyeongkwak.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function coinChange(coins: number[], amount: number): number {
2+
/*
3+
O(n)
4+
O(n)
5+
*/
6+
if (amount === 0) return 0
7+
8+
const dp = new Array(amount + 1).fill(Infinity)
9+
dp[0] = 0
10+
for(let coin of coins) {
11+
for (let idx = coin; idx <= amount; idx++) {
12+
dp[idx] = Math.min(dp[idx], dp[idx - coin] + 1)
13+
}
14+
}
15+
if (dp[amount] === Infinity) {
16+
return -1
17+
} else {
18+
return dp[amount]
19+
}
20+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
function findMin(nums: number[]): number {
2+
/*
3+
space complexity: 1
4+
time complexity : nlogn
5+
*/
6+
// return nums.sort((a, b) => a - b)[0]
7+
8+
/*
9+
space complexity: 1
10+
time complexity : logn
11+
*/
12+
let left = 0
13+
let right = nums.length - 1
14+
15+
while (left < right) {
16+
const mid = left + Math.floor((right - left) / 2)
17+
if (nums[mid] > nums[right]) {
18+
left = mid + 1
19+
} else {
20+
right = mid
21+
}
22+
}
23+
return nums[left]
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
/*
16+
time complexity : O(n)
17+
space complexity : O(logn)
18+
*/
19+
20+
function maxDepth(root: TreeNode | null): number {
21+
if (root == null) return 0
22+
let maxLeft = maxDepth(root.left)
23+
let maxRight = maxDepth(root.right)
24+
return Math.max(maxLeft, maxRight) + 1
25+
};

merge-two-sorted-lists/hoyeongkwak.ts

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* class ListNode {
4+
* val: number
5+
* next: ListNode | null
6+
* constructor(val?: number, next?: ListNode | null) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.next = (next===undefined ? null : next)
9+
* }
10+
* }
11+
*/
12+
13+
/*
14+
time complexity : O(m + n)
15+
space complexity : O(m + n)
16+
*/
17+
18+
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
19+
if (list1 == null) return list2
20+
if (list2 == null) return list1
21+
22+
if (list1.val < list2.val) {
23+
list1.next = mergeTwoLists(list1.next, list2)
24+
return list1
25+
} else {
26+
list2.next = mergeTwoLists(list1, list2.next)
27+
return list2
28+
}
29+
30+
};

word-search/hoyeongkwak.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
function exist(board: string[][], word: string): boolean {
2+
/*
3+
O(m * n)
4+
O(m * n)
5+
*/
6+
const rowsLen = board.length
7+
const colsLen = board[0].length
8+
const direction = [[0, 1], [1, 0], [0, -1], [-1, 0]]
9+
const visited = Array(rowsLen).fill(0).map(() => Array(colsLen).fill(false))
10+
const dfs = (row: number, col: number, idx: number): boolean => {
11+
if (idx === word.length)
12+
return true
13+
if (row < 0 || row >= rowsLen || col < 0 ||
14+
col >= colsLen || board[row][col] !== word[idx] || visited[row][col])
15+
return false
16+
visited[row][col] = true
17+
if (idx === word.length - 1) return true
18+
for (const [dx, dy] of direction) {
19+
const newRow = row + dx
20+
const newCol = col + dy
21+
22+
if (newRow >= 0 && newRow < rowsLen && newCol >= 0 && newCol < colsLen && !visited[newRow][newCol] && board[newRow][newCol] === word[idx + 1]) {
23+
if (dfs(newRow, newCol, idx + 1)) {
24+
return true
25+
}
26+
}
27+
}
28+
visited[row][col] = false
29+
return false
30+
}
31+
32+
for (let r = 0; r < rowsLen; r++) {
33+
for(let c = 0; c < colsLen; c++) {
34+
if (board[r][c] === word[0] && dfs(r, c, 0)) {
35+
return true
36+
}
37+
}
38+
}
39+
return false
40+
};

0 commit comments

Comments
 (0)