Skip to content

Commit 126607d

Browse files
authored
Merge pull request #1356 from JANGSEYEONG/main
[JANGSEYEONG] WEEK 04 solutions
2 parents b992406 + 41be1fb commit 126607d

File tree

5 files changed

+170
-0
lines changed

5 files changed

+170
-0
lines changed

β€Žcoin-change/JANGSEYEONG.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @param {number[]} coins
3+
* @param {number} amount
4+
* @return {number}
5+
*/
6+
var coinChange = function (coins, amount) {
7+
// f(n) = n을 λ§Œλ“œλŠ”λ° ν•„μš”ν•œ λ™μ „μ˜ μ΅œμ†Œ 개수
8+
// f(n) = min(f(n), f(n-coin) + 1)
9+
10+
// dp λ°°μ—΄ μ΄ˆκΈ°ν™”: λͺ¨λ“  κΈˆμ•‘μ„ λ§Œλ“œλŠ”λ° ν•„μš”ν•œ 동전 개수λ₯Ό λΆˆκ°€λŠ₯ν•œ κ°’(amount+1)으둜 μ„€μ •
11+
const dp = new Array(amount + 1).fill(amount + 1);
12+
dp[0] = 0; // 0원은 0개의 λ™μ „μœΌλ‘œ λ§Œλ“€ 수 있음
13+
coins.forEach((coin) => {
14+
for (let i = coin; i < dp.length; i++) {
15+
// dp[i]: 기쑴에 κ³„μ‚°λœ i원을 λ§Œλ“œλŠ” μ΅œμ†Œ 동전 개수
16+
// dp[i-coin] + 1: (i-coin)원에 ν˜„μž¬ 동전 ν•˜λ‚˜λ₯Ό μΆ”κ°€ν•˜μ—¬ i원을 λ§Œλ“œλŠ” 경우
17+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
18+
}
19+
});
20+
// λͺ©ν‘œ κΈˆμ•‘μ„ λ§Œλ“€ 수 μ—†μœΌλ©΄ -1 λ°˜ν™˜, κ°€λŠ₯ν•˜λ©΄ μ΅œμ†Œ 동전 개수 λ°˜ν™˜
21+
return dp[amount] < amount + 1 ? dp[amount] : -1;
22+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*var findMin = function (nums) {
2+
while (nums.length !== 1) {
3+
if (nums[0] > nums[nums.length - 1]) {
4+
// μ•žμ—μ„œ ν•˜λ‚˜μ”© 자λ₯΄κΈ°
5+
nums = nums.slice(1, nums.length);
6+
} else {
7+
return nums[0];
8+
}
9+
}
10+
return nums[0];
11+
};*/
12+
/**
13+
* μœ„μ˜ ν’€μ΄μ²˜λŸΌ ν’€λ©΄ μ΅œμ•…μ˜ 경우 O(n)이 λ˜μ–΄λ²„λ¦Ό. μ–‘ 끝을 ν¬μΈν„°λ‘œ κ°€λ¦¬ν‚€λ©΄μ„œ 이진탐색
14+
*/
15+
/**
16+
* @param {number[]} nums
17+
* @return {number}
18+
*/
19+
var findMin = function (nums) {
20+
let left = 0;
21+
let right = nums.length - 1;
22+
23+
while (left < right) {
24+
let mid = Math.floor((left + right) / 2);
25+
if (nums[mid] > nums[right]) {
26+
left = mid + 1;
27+
} else {
28+
right = mid;
29+
}
30+
}
31+
32+
return nums[left];
33+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {number}
12+
*/
13+
var maxDepth = function (root) {
14+
if (!root) return 0; // λ…Έλ“œκ°€ μ—†μœΌλ©΄ κΉŠμ΄λŠ” 0
15+
16+
// ν˜„μž¬ λ…Έλ“œκ°€ μ‘΄μž¬ν•˜λ―€λ‘œ κΈ°λ³Έ κΉŠμ΄λŠ” 1
17+
// μžμ‹μ΄ μ•„μ˜ˆ μ—†λŠ” 경우λ₯Ό λŒ€λΉ„ν•΄ μ΄ˆκΈ°κ°’ μ„€μ •
18+
let left = 1;
19+
let right = 1;
20+
21+
// μ™Όμͺ½ μ„œλΈŒνŠΈλ¦¬κ°€ 있으면 μž¬κ·€μ μœΌλ‘œ 깊이 계산 ν›„ ν˜„μž¬ 레벨(+1) μΆ”κ°€
22+
if (root.left) {
23+
left = maxDepth(root.left) + 1;
24+
}
25+
// 였λ₯Έμͺ½ μ„œλΈŒνŠΈλ¦¬κ°€ 있으면 μž¬κ·€μ μœΌλ‘œ 깊이 계산 ν›„ ν˜„μž¬ 레벨(+1) μΆ”κ°€
26+
if (root.right) {
27+
right = maxDepth(root.right) + 1;
28+
}
29+
// μ™Όμͺ½κ³Ό 였λ₯Έμͺ½ μ„œλΈŒνŠΈλ¦¬ 쀑 더 κΉŠμ€ 값을 λ°˜ν™˜
30+
return Math.max(left, right);
31+
};
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
* @param {ListNode} list1
10+
* @param {ListNode} list2
11+
* @return {ListNode}
12+
*/
13+
var mergeTwoLists = function (list1, list2) {
14+
const answer = new ListNode();
15+
let temp = answer; // answer은 headλ₯Ό κ°€μ§€κ³  μžˆμ–΄μ•Όν•¨
16+
17+
// list1, list2 쀑 ν•˜λ‚˜λΌλ„ λκΉŒμ§€ 도달할 λ•Œ κΉŒμ§€ 루프 돌리기
18+
while (list1 && list2) {
19+
// 더 μž‘μ€μˆ˜λ₯Ό κ°€μ§„ 리슀트λ₯Ό μ—°κ²°
20+
if (list1.val < list2.val) {
21+
temp.next = list1;
22+
list1 = list1.next;
23+
} else {
24+
temp.next = list2;
25+
list2 = list2.next;
26+
}
27+
temp = temp.next;
28+
}
29+
30+
// list1, list2 쀑 μ–΄λ–€κ²Œ λκΉŒμ§€ κ°”λŠ”μ§€ λͺ°λΌμ„œ λ‘˜ λ‹€ 체크
31+
// 남은건 κ·Έλƒ₯ 뒀에 κ°€μ Έλ‹€κ°€ 뢙이기
32+
temp.next = list1 || list2;
33+
return answer.next;
34+
};

β€Žword-search/JANGSEYEONG.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @param {character[][]} board
3+
* @param {string} word
4+
* @return {boolean}
5+
*/
6+
// DFS μ‚¬μš© 이유: ν•œ 경둜λ₯Ό λκΉŒμ§€ 탐색해야 ν•˜κ³ , κ²½λ‘œλ³„λ‘œ 독립적인 λ°©λ¬Έ μƒνƒœκ°€ ν•„μš”ν•˜κΈ° λ•Œλ¬Έ
7+
// BFS λΆˆκ°€ 이유: μ—¬λŸ¬ 경둜λ₯Ό λ™μ‹œμ— νƒμƒ‰ν•˜λ©΄μ„œ λ°©λ¬Έ μƒνƒœκ°€ μ„žμ—¬ μ˜¬λ°”λ₯Έ 경둜λ₯Ό 놓칠 수 있음
8+
var exist = function (board, word) {
9+
for (let y = 0; y < board.length; y++) {
10+
for (let x = 0; x < board[0].length; x++) {
11+
// μ‹œμž‘μ΄ λ˜λŠ” 단어λ₯Ό 마주치면 dfs 돌렀보기
12+
if (board[y][x] === word[0] && dfs(board, y, x, word, 0)) {
13+
return true;
14+
}
15+
}
16+
}
17+
return false;
18+
};
19+
20+
function dfs(board, y, x, word, index) {
21+
// 성곡 쑰건: λͺ¨λ“  문자λ₯Ό μ°Ύμ•˜μ„ λ•Œ
22+
if (index === word.length) return true;
23+
24+
// μ‹€νŒ¨ 쑰건: λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜κ±°λ‚˜ ν˜„μž¬ κΈ€μžκ°€ μΌμΉ˜ν•˜μ§€ μ•Šμ„ λ•Œ
25+
if (
26+
y < 0 ||
27+
y >= board.length ||
28+
x < 0 ||
29+
x >= board[0].length ||
30+
board[y][x] !== word[index]
31+
) {
32+
return false;
33+
}
34+
35+
// ν˜„μž¬ μ…€ μ‚¬μš© ν‘œμ‹œ
36+
const temp = board[y][x];
37+
board[y][x] = true; // μž„μ‹œ λ°©λ¬Έ ν‘œμ‹œ
38+
39+
// μƒν•˜μ’Œμš° 탐색, ν•˜λ‚˜λΌλ„ μ°Ύκ²Œλœλ‹€λ©΄ true
40+
const found =
41+
dfs(board, y + 1, x, word, index + 1) ||
42+
dfs(board, y - 1, x, word, index + 1) ||
43+
dfs(board, y, x + 1, word, index + 1) ||
44+
dfs(board, y, x - 1, word, index + 1);
45+
46+
// μ›λž˜ κ°’μœΌλ‘œ 볡원 (λ°±νŠΈλž˜ν‚Ή)
47+
board[y][x] = temp;
48+
49+
return found;
50+
}

0 commit comments

Comments
Β (0)