Skip to content

Commit cc63aaa

Browse files
authored
Merge pull request #1346 from uraflower/main
[uraflower] WEEK 04 solutions
2 parents d13ec0d + 12117a4 commit cc63aaa

File tree

5 files changed

+172
-0
lines changed

5 files changed

+172
-0
lines changed

โ€Žcoin-change/uraflower.jsโ€Ž

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๊ธˆ์•ก์„ ๋™์ „์œผ๋กœ ๊ฑฐ์Šค๋ฅผ ๋•Œ ์ตœ์†Œ ๋™์ „ ๊ฐœ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number[]} coins
4+
* @param {number} amount
5+
* @return {number}
6+
*/
7+
const coinChange = function (coins, amount) {
8+
const dp = [0, ...Array(amount).fill(amount + 1)];
9+
10+
for (let coin of coins) {
11+
for (let n = coin; n <= amount; n++) {
12+
dp[n] = Math.min(dp[n], dp[n - coin] + 1)
13+
}
14+
}
15+
16+
return dp[amount] > amount ? -1 : dp[amount]
17+
};
18+
19+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(c*n) (c: coins.length, n: amount)
20+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๋ฐฐ์—ด์˜ ์ตœ์†Ÿ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {number[]} nums
4+
* @return {number}
5+
*/
6+
// ์ฒซ ๋ฒˆ์งธ ์‹œ๋„
7+
const findMin = function(nums) {
8+
return Math.min(...nums);
9+
};
10+
11+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
12+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
13+
14+
// ===========================================
15+
// ๋‘ ๋ฒˆ์งธ ์‹œ๋„
16+
const findMin = function(nums) {
17+
let left = 0;
18+
let right = nums.length - 1;
19+
let mid;
20+
21+
while (left < right) {
22+
mid = Math.floor((left + right) / 2);
23+
24+
if (nums[mid] < nums[right]) {
25+
right = mid;
26+
} else {
27+
left = mid + 1;
28+
}
29+
}
30+
31+
return nums[left];
32+
}
33+
34+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(logn)
35+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(1)
Lines changed: 31 additions & 0 deletions
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+
* ์ฃผ์–ด์ง„ ์ด์ง„ ํŠธ๋ฆฌ์˜ ์ตœ๋Œ€ ๊นŠ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
const maxDepth = function(root) {
15+
let maxDepth = 0;
16+
17+
function bfs(node, depth) {
18+
if (!node) return;
19+
20+
depth += 1;
21+
if (maxDepth < depth) maxDepth = depth;
22+
bfs(node.left, depth);
23+
bfs(node.right, depth);
24+
}
25+
26+
bfs(root, maxDepth);
27+
return maxDepth;
28+
};
29+
30+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
31+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(h) (h: ํŠธ๋ฆฌ์˜ ๋†’์ด. ์ตœ์•…์˜ ๊ฒฝ์šฐ ํŽธํ–ฅํŠธ๋ฆฌ์ผ ๋•Œ h===n์œผ๋กœ O(n))
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* Definition for singly-linked list.
3+
*/
4+
function ListNode(val, next) {
5+
this.val = (val === undefined ? 0 : val)
6+
this.next = (next === undefined ? null : next)
7+
}
8+
9+
/**
10+
* ์ฃผ์–ด์ง„ ๋‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ ์ •๋ ฌ ๋ฆฌ์ŠคํŠธ๋กœ ๋ณ‘ํ•ฉํ•ด ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
11+
* @param {ListNode} list1
12+
* @param {ListNode} list2
13+
* @return {ListNode}
14+
*/
15+
const mergeTwoLists = function (list1, list2) {
16+
let head = new ListNode(-1, null);
17+
let mergedList = head;
18+
19+
let node1 = list1;
20+
let node2 = list2;
21+
22+
while (node1 && node2) {
23+
if (node1.val >= node2.val) {
24+
mergedList.next = node2;
25+
node2 = node2.next;
26+
} else if (node1.val < node2.val) {
27+
mergedList.next = node1;
28+
node1 = node1.next;
29+
}
30+
31+
mergedList = mergedList.next;
32+
}
33+
34+
mergedList.next = node1 ?? node2;
35+
36+
return head.next;
37+
};
38+
39+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(n)
40+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(n)

โ€Žword-search/uraflower.jsโ€Ž

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ 2์ฐจ ๋ฐฐ์—ด์— word๊ฐ€ ์žˆ๋Š”์ง€ ์—ฌ๋ถ€๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜
3+
* @param {character[][]} board
4+
* @param {string} word
5+
* @return {boolean}
6+
*/
7+
const exist = function (board, word) {
8+
const row = board.length;
9+
const col = board[0].length;
10+
const visited = Array.from({ length: row }, () => Array(col).fill(false));
11+
const dr = [0, 0, 1, -1];
12+
const dc = [1, -1, 0, 0];
13+
14+
function dfs(r, c, charIdx) {
15+
16+
if (charIdx + 1 === word.length) return true;
17+
18+
for (let i = 0; i < 4; i++) {
19+
const nr = r + dr[i];
20+
const nc = c + dc[i];
21+
22+
if (0 <= nr && nr < row && 0 <= nc && nc < col && !visited[nr][nc] && word[charIdx + 1] === board[nr][nc]) {
23+
visited[nr][nc] = true;
24+
if (dfs(nr, nc, charIdx + 1)) return true;
25+
visited[nr][nc] = false;
26+
}
27+
}
28+
29+
return false;
30+
}
31+
32+
for (let r = 0; r < row; r++) {
33+
for (let c = 0; c < col; c++) {
34+
if (word[0] === board[r][c]) {
35+
visited[r][c] = true;
36+
if (dfs(r, c, 0)) return true;
37+
visited[r][c] = false;
38+
}
39+
}
40+
}
41+
42+
return false;
43+
};
44+
45+
// ์‹œ๊ฐ„๋ณต์žก๋„: O(m * n * 4^L) (L = word.length. ๋„ค ๋ฐฉํ–ฅ์œผ๋กœ ์ตœ๋Œ€ L๋งŒํผ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ์Œ.)
46+
// ๊ณต๊ฐ„๋ณต์žก๋„: O(m * n)

0 commit comments

Comments
ย (0)