Skip to content

Commit 5bd0712

Browse files
authored
Merge pull request #1821 from jun0811/main
[jun0811] WEEK 04 Solutions
2 parents 7f52ecd + 7ca169f commit 5bd0712

File tree

5 files changed

+129
-0
lines changed

5 files changed

+129
-0
lines changed

coin-change/jun0811.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var coinChange = function(coins, amount) {
2+
if(amount == 0) return 0
3+
4+
const dp = [0, ...new Array(amount).fill(amount+1)]
5+
6+
for (const coin of coins) {
7+
for (let i = coin; i <=amount; i++) {
8+
dp[i] = Math.min(dp[i], dp[i-coin] + 1)
9+
}
10+
}
11+
12+
return dp[amount] < amount+1 ? dp[amount] : -1
13+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
var findMin = function (nums) {
2+
let left = 0,
3+
right = nums.length - 1;
4+
5+
while (left < right) {
6+
const mid = Math.floor((left + right) / 2);
7+
8+
if (nums[mid] > nums[right]) {
9+
left = mid + 1; // 최솟값이 오른쪽에 있음
10+
} else {
11+
right = mid; // 최솟값이 왼쪽에 있음 (mid 포함)
12+
}
13+
}
14+
15+
return nums[left];
16+
};
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var maxDepth = function (root) {
2+
let res = 0;
3+
4+
if (!root) return res; // root 자체가 없을 경우만 0
5+
6+
function check(node, depth) {
7+
if (!node.left && !node.right) {
8+
res = Math.max(res, depth);
9+
return;
10+
}
11+
12+
if (node.left) {
13+
check(node.left, depth + 1);
14+
}
15+
if (node.right) {
16+
check(node.right, depth + 1);
17+
}
18+
}
19+
20+
check(root, 1); // 루트부터 시작
21+
return res;
22+
};

merge-two-sorted-lists/jun0811.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* function ListNode(val, next) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.next = (next===undefined ? null : next)
6+
* }
7+
*/
8+
9+
var mergeTwoLists = function (list1, list2) {
10+
if (!list1) return list2;
11+
else if (!list2) return list1;
12+
13+
if (list1.val <= list2.val) {
14+
list1.next = mergeTwoLists(list1.next, list2);
15+
return list1;
16+
} else {
17+
list2.next = mergeTwoLists(list1, list2.next);
18+
return list2;
19+
}
20+
};

word-search/jun0811.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
7+
const directions = [
8+
[0, -1],
9+
[1, 0],
10+
[-1, 0],
11+
[0, 1],
12+
];
13+
14+
var exist = function (board, word) {
15+
const cols = board[0].length; // 가로 (열 개수)
16+
const rows = board.length; // 세로 (행 개수)
17+
let res = false;
18+
19+
for (let col = 0; col < cols; col++) {
20+
for (let row = 0; row < rows; row++) {
21+
if (board[row][col] != word[0]) continue;
22+
23+
const visited = Array.from({ length: rows }, () =>
24+
Array(cols).fill(false)
25+
);
26+
if (res) break;
27+
dfs(row, col, board[row][col], visited);
28+
}
29+
}
30+
31+
function check(row, col) {
32+
if (!(row >= 0 && row < rows)) return false;
33+
if (!(col >= 0 && col < cols)) return false;
34+
return true;
35+
}
36+
37+
function dfs(row, col, str, visited) {
38+
if (str == word) {
39+
res = true;
40+
return;
41+
}
42+
if (str.length >= word.length) return;
43+
visited[row][col] = true;
44+
45+
for (const direction of directions) {
46+
const [d_r, d_c] = direction;
47+
const newCol = col + d_c;
48+
const newRow = row + d_r;
49+
50+
if (check(newRow, newCol) && !visited[newRow][newCol]) {
51+
dfs(newRow, newCol, str + board[newRow][newCol], visited);
52+
}
53+
}
54+
visited[row][col] = false;
55+
}
56+
57+
return res;
58+
};

0 commit comments

Comments
 (0)