Skip to content

Commit fd2737b

Browse files
authored
Merge pull request #2143 from Blossssom/main
[Blossssom] WEEK 04 solutions
2 parents bd28048 + 7b80412 commit fd2737b

File tree

5 files changed

+236
-0
lines changed

5 files changed

+236
-0
lines changed

โ€Žcoin-change/Blossssom.tsโ€Ž

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* @param coins - ๋™์ „ ์ข…๋ฅ˜ ๋ฐฐ์—ด
3+
* @param amount - ์ด ๊ธˆ์•ก
4+
* @returns - ์ด ์‚ฌ์šฉ ์ฝ”์ธ ๊ฐฏ์ˆ˜
5+
* @description
6+
* - ํƒ์š•์œผ๋กœ ํ’€๋ฉด ์ตœ์†Œ ๋™์ „๊ฐฏ์ˆ˜๋ฅผ ๊ตฌํ•  ์ˆ˜ ์—†์Œ
7+
* - dp ๋กœ ํ’€์ด
8+
* - 1 ๋ถ€ํ„ฐ ๊ธˆ์•ก์˜ ์ฝ”์ธ ์‚ฌ์šฉ ๊ฐฏ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•ด ๊ฐ€๋ฉฐ, ์ด์ „์— ๊ตฌํ•ด๋†“์€ ๊ฐ’๊ณผ min ๋น„๊ต
9+
*/
10+
function coinChange(coins: number[], amount: number): number {
11+
const dp = new Array(amount + 1).fill(Infinity);
12+
13+
dp[0] = 0;
14+
15+
for (let i = 1; i <= amount; i++) {
16+
for (const coin of coins) {
17+
if (i - coin >= 0) {
18+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
19+
}
20+
}
21+
}
22+
23+
return dp[amount] === Infinity ? -1 : dp[amount];
24+
}
25+
26+
const coins = [1, 2, 5];
27+
const amount = 11;
28+
coinChange(coins, amount);
29+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @param nums - ์˜ค๋ฆ„์ฐจ์ˆœ ์ •๋ ฌ๋œ ์ •์ˆ˜ ๋ฐฐ์—ด
3+
* @returns - ์ตœ์†Ÿ๊ฐ’
4+
* @description
5+
* - ์ด์ง„ ํƒ์ƒ‰์œผ๋กœ right์™€ ๋น„๊ตํ•˜๋ฉฐ ๋ฐฐ์—ด์˜ ๋ฒ”์œ„๋ฅผ ์ขํ˜€๊ฐ€๋ฉฐ ํƒ์ƒ‰
6+
* - left์™€ right๊ฐ€ ๊ฒน์น˜๋Š” ํฌ์ธํŠธ๊ฐ€ ์ตœ์†Ÿ๊ฐ’
7+
*/
8+
9+
function findMin(nums: number[]): number {
10+
let left = 0;
11+
let mid = Math.floor(nums.length / 2);
12+
let right = nums.length - 1;
13+
14+
if (nums.length < 2) {
15+
return nums[0];
16+
}
17+
18+
while (left < right) {
19+
if (nums[right] < nums[mid]) {
20+
left = mid + 1;
21+
} else {
22+
right = mid;
23+
}
24+
25+
mid = Math.floor((left + right) / 2);
26+
}
27+
return nums[left];
28+
}
29+
30+
const nums = [2, 1];
31+
console.log(findMin(nums));
32+
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class TreeNode {
2+
val: number;
3+
left: TreeNode | null;
4+
right: TreeNode | null;
5+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
6+
this.val = val === undefined ? 0 : val;
7+
this.left = left === undefined ? null : left;
8+
this.right = right === undefined ? null : right;
9+
}
10+
}
11+
12+
/**
13+
* @param root - ์ด์ง„ ํŠธ๋ฆฌ
14+
* @returns - ๋ฃจํŠธ ๋…ธ๋“œ์—์„œ ๊ฐ€์žฅ ๋จผ ๋ฆฌํ”„๋…ธ๋“œ ๊นŒ์ง€ ๊ฐ€์žฅ ๊ธด ๊ฒฝ๋กœ๋ฅผ ๋”ฐ๋ฅธ ๋…ธ๋“œ ์ˆ˜
15+
* @description
16+
* - root๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธ ํ›„ ์žฌ๊ท€ ํ˜ธ์ถœ๋กœ 1์”ฉ ์ฆ๊ฐ€
17+
* - left, right ์ค‘ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ return
18+
* - maxDepth ์ž์ฒด๋ฅผ ์žฌ๊ท€๋กœ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์ด ๊ฐ€์žฅ ํšจ์œจ์ 
19+
*/
20+
21+
// function maxDepth(root: TreeNode | null): number {
22+
// if (!root) {
23+
// return 0;
24+
// }
25+
26+
// function recursive(current: TreeNode | null): number {
27+
// if (!current) {
28+
// return 0;
29+
// }
30+
31+
// return 1 + Math.max(recursive(current.left), recursive(current.right));
32+
// }
33+
34+
// return recursive(root);
35+
// }
36+
37+
function maxDepth(root: TreeNode | null): number {
38+
if (root === null) {
39+
return 0;
40+
}
41+
42+
const left = 1 + maxDepth(root.left);
43+
const right = 1 + maxDepth(root.right);
44+
45+
return Math.max(left, right);
46+
}
47+
48+
const root = new TreeNode(
49+
3,
50+
new TreeNode(9),
51+
new TreeNode(20, new TreeNode(15), new TreeNode(7))
52+
);
53+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}
9+
10+
/**
11+
* @param linked_list list1
12+
* @param linked_list list2
13+
* @returns ์ •๋ ฌ๋œ linked_list
14+
* @description
15+
* - ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ฐ ๋‹จ๊ณ„๋ฅผ ํƒ์ƒ‰ํ•˜๋ ค๋‹ˆ ์ ‘๊ทผ ๋ฐฉ๋ฒ•์ด ๋– ์˜ค๋ฅด์ง€ ์•Š์Œ
16+
* - ํ•ด๋‹น ์œ ํ˜•์˜ ๋ฌธ์ œ๋ฅผ ๋” ํ’€์–ด๋ณผ ์˜ˆ์ •
17+
*/
18+
19+
function mergeTwoLists(
20+
list1: ListNode | null,
21+
list2: ListNode | null
22+
): ListNode | null {
23+
if (!list1 && !list2) {
24+
return null;
25+
}
26+
27+
const temp = new ListNode();
28+
let current = temp;
29+
30+
while (list1 && list2) {
31+
if (list1.val < list2.val) {
32+
current.next = list1;
33+
list1 = list1.next;
34+
} else {
35+
current.next = list2;
36+
list2 = list2.next;
37+
}
38+
current = current.next;
39+
}
40+
current.next = list1 || list2;
41+
42+
return temp.next;
43+
}
44+
45+
const list1 = new ListNode(1, new ListNode(2, new ListNode(4)));
46+
const list2 = new ListNode(1, new ListNode(3, new ListNode(4)));
47+
48+
mergeTwoLists(list1, list2);
49+

โ€Žword-search/Blossssom.tsโ€Ž

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* @param board - m * n ๋ฌธ์ž ๊ทธ๋ฆฌ๋“œ
3+
* @param word - ์ฐพ์„ ๋‹จ์–ด
4+
* @returns - ๊ทธ๋ฆฌ๋“œ ๋‚ด ์ธ์ ‘ํ•œ ์…€์˜ ๊ธ€์ž๋กœ word๋ฅผ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š”์ง€ ๋ฐ˜ํ™˜
5+
* @description
6+
* - ๋™์ผํ•œ ์…€ ๊ธ€์ž๋Š” ํ•œ ๋ฒˆ๋งŒ ์‚ฌ์šฉ๊ฐ€๋Šฅ
7+
* - visit ์ฒดํฌ ๋ถ€๋ถ„์—์„œ ์กฐ๊ธˆ ์ƒ๊ฐ์„ ์ž˜๋ชปํ•ด ์˜ค๋ž˜๊ฑธ๋ฆผ
8+
* - dfs ์—ฐ์Šต ํ•„์š”, ๋ฌธ์ œ๋ฅผ ์ตœ๋Œ€ํ•œ ์ž˜๊ฒŒ ์ž˜๋ผ์„œ ์ƒ๊ฐํ•ด๋ณด๊ธฐ
9+
*/
10+
11+
function exist(board: string[][], word: string): boolean {
12+
const maximumRow = board.length - 1;
13+
const maximumCol = board[0].length - 1;
14+
// ์‹œ์ž‘์ง€์  ์ฐพ๊ธฐ
15+
const starter = board.reduce<number[][]>((acc, row, rIndex) => {
16+
row.forEach((col, cIndex) => {
17+
if (col === word[0]) {
18+
acc.push([rIndex, cIndex]);
19+
}
20+
});
21+
return acc;
22+
}, []);
23+
24+
function recursive(row: number, col: number, target: number): boolean {
25+
// word์˜ ๊ธธ์ด๊นŒ์ง€ ์™”๋‹ค๋ฉด ์ด์ „ ์กฐ๊ฑด์€ ํ†ต๊ณผ ์ฆ‰, word์™€ ๋™์ผ ๋ฌธ์ž์—ด
26+
if (target === word.length) {
27+
return true;
28+
}
29+
30+
// ์ธ์ ‘ index ๋ฒ”์œ„ ์ฒดํฌ ๋ฐ ๊ฐ’ ์ฒดํฌ
31+
if (
32+
row < 0 ||
33+
row > maximumRow ||
34+
col < 0 ||
35+
col > maximumCol ||
36+
board[row][col] !== word[target]
37+
) {
38+
return false;
39+
}
40+
// ์ด์ „ ๊ฐ’์„ ๋™์ผ ์ฒดํฌํ•˜์ง€ ์•Š๋„๋ก ๊ฐ’์„ ์ €์žฅ ๋ฐ ๋ณ€๊ฒฝ
41+
const saveValue = board[row][col];
42+
// ์ฐก๊ธ‹
43+
board[row][col] = ">_o";
44+
45+
const finding =
46+
recursive(row + 1, col, target + 1) ||
47+
recursive(row - 1, col, target + 1) ||
48+
recursive(row, col + 1, target + 1) ||
49+
recursive(row, col - 1, target + 1);
50+
51+
// ๋‹ค์Œ ์ˆœํšŒ ๋ฐ ์ฒดํฌ๋ฅผ ์œ„ํ•œ ์›์ƒ๋ณต๊ตฌ
52+
board[row][col] = saveValue;
53+
return finding;
54+
}
55+
56+
for (const start of starter) {
57+
const isFind = recursive(start[0], start[1], 0);
58+
if (isFind) {
59+
return true;
60+
}
61+
}
62+
return false;
63+
}
64+
65+
const board = [
66+
["A", "B", "C", "E"],
67+
["S", "F", "C", "S"],
68+
["A", "D", "E", "E"],
69+
];
70+
71+
const word = "ABCCED";
72+
console.log(exist(board, word));
73+

0 commit comments

Comments
ย (0)