Skip to content

Commit 301430f

Browse files
authored
Merge pull request #1345 from HoonDongKang/main
[HoonDongKang] Week 04 Solutions
2 parents c6a817c + 7eecb66 commit 301430f

File tree

5 files changed

+289
-0
lines changed

5 files changed

+289
-0
lines changed

coin-change/HoonDongKang.ts

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* [Problem]: [322] Coin Change
3+
*
4+
* (https://leetcode.com/problems/coin-change/description/)
5+
*/
6+
function coinChange(coins: number[], amount: number): number {
7+
// 시간복잡도: O(c^a)
8+
// 공간복잡도: O(a)
9+
// Time Exceed
10+
function dfsFunc(coins: number[], amount: number): number {
11+
if (!amount) return 0;
12+
let result = dfs(amount);
13+
14+
return result <= amount ? result : -1;
15+
16+
function dfs(remain: number): number {
17+
if (remain === 0) return 0;
18+
if (remain < 0) return amount + 1;
19+
20+
let min_count = amount + 1;
21+
22+
for (let coin of coins) {
23+
const result = dfs(remain - coin);
24+
min_count = Math.min(min_count, result + 1);
25+
}
26+
27+
return min_count;
28+
}
29+
}
30+
// 시간복잡도: O(ca)
31+
// 공간복잡도: O(a)
32+
function dpFunc(coins: number[], amount: number): number {
33+
const dp = new Array(amount + 1).fill(amount + 1);
34+
dp[0] = 0;
35+
36+
for (let coin of coins) {
37+
for (let i = coin; i <= amount; i++) {
38+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
39+
}
40+
}
41+
42+
return dp[amount] <= amount ? dp[amount] : -1;
43+
}
44+
45+
// 시간복잡도: O(ca)
46+
// 공간복잡도: O(a)
47+
function memoizationFunc(coins: number[], amount: number): number {
48+
const memo: Record<number, number> = {};
49+
50+
const result = dfs(amount);
51+
return result <= amount ? result : -1;
52+
53+
function dfs(remain: number): number {
54+
if (remain === 0) return 0;
55+
if (remain < 0) return amount + 1;
56+
if (remain in memo) return memo[remain];
57+
58+
let min_count = amount + 1;
59+
60+
for (let coin of coins) {
61+
const res = dfs(remain - coin);
62+
min_count = Math.min(min_count, res + 1);
63+
}
64+
65+
memo[remain] = min_count;
66+
67+
return min_count;
68+
}
69+
}
70+
71+
return memoizationFunc(coins, amount);
72+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* [Problem]: [153] Find Minimum in Rotated Sorted Array
3+
*
4+
* (https://leetcode.com/problems/find-minimum-in-rotated-sorted-array/description/)
5+
*/
6+
7+
function findMin(nums: number[]): number {
8+
// 실제 Submit은 되나, 문제의 의도와 다름
9+
function ggomsuFunc(nums: number[]): number {
10+
return Math.min(...nums);
11+
}
12+
13+
//시간복잡도 O(logn)
14+
//공간복잡도 O(1)
15+
function binarySearchFunc(nums: number[]): number {
16+
let left = 0;
17+
let right = nums.length - 1;
18+
19+
while (left < right) {
20+
const mid = Math.floor((left + right) / 2);
21+
if (nums[mid] > nums[right]) {
22+
left = mid + 1;
23+
} else {
24+
right = mid;
25+
}
26+
}
27+
28+
return nums[left];
29+
}
30+
31+
return binarySearchFunc(nums);
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* [Problem]: [104] Maximum Depth of Binary Tree
3+
*
4+
* (https://leetcode.com/problems/maximum-depth-of-binary-tree/description/)
5+
*/
6+
7+
class TreeNode {
8+
val: number;
9+
left: TreeNode | null;
10+
right: TreeNode | null;
11+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
12+
this.val = val === undefined ? 0 : val;
13+
this.left = left === undefined ? null : left;
14+
this.right = right === undefined ? null : right;
15+
}
16+
}
17+
18+
function maxDepth(root: TreeNode | null): number {
19+
//시간복잡도 O(n)
20+
//공간복잡도 O(n)
21+
function stackFunc(root: TreeNode | null): number {
22+
if (!root) return 0;
23+
let max = 0;
24+
const stack: [TreeNode, number][] = [[root, 1]];
25+
26+
while (stack.length > 0) {
27+
const [node, depth] = stack.pop()!;
28+
max = Math.max(max, depth);
29+
30+
if (node.left) stack.push([node.left, depth + 1]);
31+
if (node.right) stack.push([node.right, depth + 1]);
32+
}
33+
34+
return max;
35+
}
36+
37+
//시간복잡도 O(n)
38+
//공간복잡도 O(n)
39+
function recursiveFunc(root: TreeNode | null): number {
40+
if (!root) return 0;
41+
42+
return 1 + Math.max(recursiveFunc(root.left), recursiveFunc(root.right));
43+
}
44+
45+
return recursiveFunc(root);
46+
}
+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* [Problem]: [21] Merge Two Sorted Lists
3+
*
4+
* (https://leetcode.com/problems/merge-two-sorted-lists/description/)
5+
*/
6+
7+
class ListNode {
8+
val: number;
9+
next: ListNode | null;
10+
constructor(val?: number, next?: ListNode | null) {
11+
this.val = val === undefined ? 0 : val;
12+
this.next = next === undefined ? null : next;
13+
}
14+
}
15+
16+
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
17+
//시간복잡도 O(m+n)
18+
//공간복잡도 O(1)
19+
function loopFunc(list1: ListNode | null, list2: ListNode | null): ListNode | null {
20+
let current: ListNode = new ListNode();
21+
let dummy = current;
22+
23+
while (list1 !== null && list2 !== null) {
24+
if (list1.val < list2.val) {
25+
current.next = list1;
26+
list1 = list1.next;
27+
} else {
28+
current.next = list2;
29+
list2 = list2.next;
30+
}
31+
32+
current = current.next;
33+
}
34+
35+
current.next = list1 !== null ? list1 : list2;
36+
37+
return dummy.next;
38+
}
39+
40+
//시간복잡도 O(m+n)
41+
//공간복잡도 O(m+n)
42+
function recursionFunc(list1: ListNode | null, list2: ListNode | null): ListNode | null {
43+
if (list1 === null) return list2;
44+
if (list2 === null) return list1;
45+
46+
if (list1.val < list2.val) {
47+
list1.next = recursionFunc(list1.next, list2);
48+
return list1;
49+
} else {
50+
list2.next = recursionFunc(list1, list2.next);
51+
return list2;
52+
}
53+
}
54+
55+
return recursionFunc(list1, list2);
56+
}

word-search/HoonDongKang.ts

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* [Problem]: [79] Word Search
3+
*
4+
* (https://leetcode.com/problems/word-search/description/)
5+
*/
6+
7+
function exist(board: string[][], word: string): boolean {
8+
//시간복잡도 O(m * n * 4^w)
9+
//공간복잡도 O(w)
10+
function dfsChangingBoard(board: string[][], word: string): boolean {
11+
const rows = board.length;
12+
const cols = board[0].length;
13+
14+
for (let i = 0; i < rows; i++) {
15+
for (let j = 0; j < cols; j++) {
16+
if (dfs(i, j, 0)) return true;
17+
}
18+
}
19+
20+
return false;
21+
22+
function dfs(i: number, j: number, k: number): boolean {
23+
if (k === word.length) return true;
24+
25+
if (i < 0 || j < 0) return false;
26+
if (i >= rows || j >= cols) return false;
27+
if (board[i][j] !== word[k]) return false;
28+
29+
const temp = board[i][j];
30+
board[i][j] = "";
31+
32+
const result =
33+
dfs(i + 1, j, k + 1) ||
34+
dfs(i - 1, j, k + 1) ||
35+
dfs(i, j + 1, k + 1) ||
36+
dfs(i, j - 1, k + 1);
37+
38+
board[i][j] = temp;
39+
return result;
40+
}
41+
}
42+
43+
//시간복잡도 O(m * n * 4^w)
44+
//공간복잡도 O(w)
45+
function dfsMaintainingBoard(board: string[][], word: string): boolean {
46+
const rows = board.length;
47+
const cols = board[0].length;
48+
49+
// array보다 Set<string>이 더 효율적
50+
// Set.has: O(1) / Array.includes: O(n)
51+
const passed = new Set<string>();
52+
53+
for (let i = 0; i < rows; i++) {
54+
for (let j = 0; j < cols; j++) {
55+
if (dfs(i, j, 0)) return true;
56+
}
57+
}
58+
59+
return false;
60+
61+
function dfs(i: number, j: number, k: number): boolean {
62+
if (k === word.length) return true;
63+
64+
if (i < 0 || j < 0) return false;
65+
if (i >= rows || j >= cols) return false;
66+
if (board[i][j] !== word[k]) return false;
67+
if (passed.has(`${i},${j}`)) return false;
68+
69+
passed.add(`${i},${j}`);
70+
71+
const result =
72+
dfs(i + 1, j, k + 1) ||
73+
dfs(i - 1, j, k + 1) ||
74+
dfs(i, j + 1, k + 1) ||
75+
dfs(i, j - 1, k + 1);
76+
77+
passed.delete(`${i},${j}`);
78+
return result;
79+
}
80+
}
81+
82+
return dfsMaintainingBoard(board, word);
83+
}

0 commit comments

Comments
 (0)