Skip to content

Commit dbf65e8

Browse files
authoredMay 2, 2025
Merge pull request #1392 from clara-shin/main
[clara-shin] WEEK 05 solutions
2 parents cf65e7b + aaeb0b1 commit dbf65e8

File tree

6 files changed

+278
-0
lines changed

6 files changed

+278
-0
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* ์‹œ๊ฐ„ ๋ณต์žก๋„ O(n)
3+
* ๊ณต๊ฐ„ ๋ณต์žก๋„ O(1)
4+
*
5+
* ๊ทธ๋ฆฌ๋”” ์•Œ๊ณ ๋ฆฌ์ฆ˜
6+
* ํ˜„์žฌ๊นŒ์ง€์˜ ์ตœ์ € ๊ฐ€๊ฒฉ์„ ๊ธฐ์–ตํ•˜๊ณ , ๊ทธ ๊ฐ€๊ฒฉ์— ์ƒ€์„ ๋•Œ์˜ ์ด์ต์„ ๊ณ„์† ๊ณ„์‚ฐํ•˜์—ฌ ์ตœ๋Œ€ ์ด์ต์„ ๊ตฌํ•จ
7+
*/
8+
9+
/**
10+
* @param {number[]} prices
11+
* @return {number}
12+
*/
13+
var maxProfit = function (prices) {
14+
let minPrice = prices[0]; // ์ตœ์ € ๊ฐ€๊ฒฉ ์ดˆ๊ธฐํ™” (์ฒซ ๋‚  ๊ฐ€๊ฒฉ)
15+
let maxProfit = 0; // ์ตœ๋Œ€ ์ด์ต ์ดˆ๊ธฐํ™” (์•„์ง ์ด์ต ์—†์Œ)
16+
17+
// ๋‘ ๋ฒˆ์งธ ๋‚ ๋ถ€ํ„ฐ
18+
for (let i = 1; i < prices.length; i++) {
19+
minPrice = Math.min(minPrice, prices[i]); // ์ตœ์ € ๊ฐ€๊ฒฉ ๊ฐฑ์‹ 
20+
maxProfit = Math.max(maxProfit, prices[i] - minPrice); // ์ตœ๋Œ€ ์ด์ต ๊ฐฑ์‹ 
21+
}
22+
23+
return maxProfit;
24+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ๋ฅผ ํ•˜๋‚˜์˜ ๋ฌธ์ž์—ด๋กœ ์ธ์ฝ”๋”ฉํ•˜๊ณ , ๋‹ค์‹œ ๋””์ฝ”๋”ฉ ํ•˜๊ธฐ
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ•:
5+
* ๊ธธ์ด ๊ธฐ๋ฐ˜ ์ธ์ฝ”๋”ฉ => ๊ฐ ๋ฌธ์ž์—ด๋งˆ๋‹ค "๊ธธ์ด#๋ฌธ์ž์—ด" ํ˜•์‹์œผ๋กœ ์ธ์ฝ”๋”ฉ
6+
* "#"์„ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๋‚˜๋ˆ„๊ณ , ๊ธธ์ด๋ฅผ ์ด์šฉํ•ด ์›๋ž˜ ๋ฌธ์ž์—ด์„ ๋ณต์›ํ•˜์—ฌ ๋””์ฝ”๋”ฉ
7+
*/
8+
9+
/**
10+
* @param {string[]} strs - ์ธ์ฝ”๋”ฉํ•  ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ
11+
* @return {string} - ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด
12+
*/
13+
var encode = function (strs) {
14+
if (!strs || strs.length === 0) return '';
15+
return strs.map((str) => str.length + '#' + str).join('');
16+
};
17+
18+
/**
19+
* @param {string} s - ๋””์ฝ”๋”ฉํ•  ์ธ์ฝ”๋”ฉ๋œ ๋ฌธ์ž์—ด
20+
* @return {string[]} - ์›๋ž˜์˜ ๋ฌธ์ž์—ด ๋ฆฌ์ŠคํŠธ
21+
*/
22+
var decode = function (s) {
23+
if (!s || s.length === 0) return [];
24+
25+
const result = [];
26+
let i = 0;
27+
28+
while (i < s.length) {
29+
const delimiterIndex = s.indexOf('#', i); // '#'์˜ ์œ„์น˜ ์ฐพ๊ธฐ
30+
const length = parseInt(s.slice(i, delimiterIndex)); // ๊ธธ์ด ์ถ”์ถœ
31+
32+
const start = delimiterIndex + 1; // ๋ฌธ์ž์—ด ์‹œ์ž‘ ์œ„์น˜
33+
result.push(s.slice(start, start + length)); // ์•Œ์•„๋‚ธ ๊ธธ์ด๋งŒํผ ๋ฌธ์ž์—ด ์ถ”์ถœ ํ›„ ๊ฒฐ๊ณผ์— ์ถ”๊ฐ€
34+
i = start + length; // ๋‹ค์Œ ๋ฌธ์ž์—ด ์‹œ์ž‘ ์œ„์น˜๋กœ ํฌ์ธํ„ฐ ์ด๋™
35+
}
36+
37+
return result;
38+
};

โ€Žgroup-anagrams/clara-shin.js

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* ๋ฌธ์ž์—ด ๋ฐฐ์—ด strs๊ฐ€ ์ฃผ์–ด์กŒ์„ ๋•Œ, ์• ๋„ˆ๊ทธ๋žจ๋ผ๋ฆฌ ๊ทธ๋ฃนํ™”ํ•˜๋Š” ๋ฌธ์ œ
3+
* ์• ๋„ˆ๊ทธ๋žจ: ๊ฐ™์€ ๋ฌธ์ž๋ฅผ ์žฌ๋ฐฐ์—ดํ•˜์—ฌ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๋“ค
4+
*
5+
* ์ ‘๊ทผ ๋ฐฉ๋ฒ•: ๊ฐ ๋ฌธ์ž ์ถœํ˜„๋นˆ๋„๋ฅผ ์นด์šดํŒ…ํ•˜๋Š” ๋ฐฉ์‹
6+
* 1. ๊ฐ ๋ฌธ์ž์—ด์„ ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ‚ค๋ฅผ ์ƒ์„ฑ
7+
* 2. Map์„ ์‚ฌ์šฉํ•˜์—ฌ ํ‚ค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ๋ฌธ์ž์—ด์„ ๊ทธ๋ฃนํ™”
8+
* 3. Map์˜ ๊ฐ’๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
9+
*
10+
* ์‹œ๊ฐ„๋ณต์žก๋„: O(n * k) (n: ๋ฌธ์ž์—ด ๊ฐœ์ˆ˜, k: ๋ฌธ์ž์—ด ๊ธธ์ด) -> ๋‹จ์ˆœ ์นด์šดํŒ…
11+
* ๊ณต๊ฐ„๋ณต์žก๋„: O(n * k) -> ๋ชจ๋“  ๋ฌธ์ž์—ด์„ ์ €์žฅํ•ด์•ผ ํ•˜๋ฏ€๋กœ
12+
*/
13+
14+
/**
15+
* @param {string[]} strs
16+
* @return {string[][]}
17+
*/
18+
var groupAnagrams = function (strs) {
19+
const map = new Map();
20+
21+
for (let str of strs) {
22+
const count = new Array(26).fill(0); // ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜ ์ดˆ๊ธฐํ™”
23+
for (let i = 0; i < str.length; i++) {
24+
const index = str.charCodeAt(i) - 'a'.charCodeAt(0); // ์•ŒํŒŒ๋ฒณ ์ธ๋ฑ์Šค ๊ณ„์‚ฐ
25+
count[index]++; // ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜ ์ฆ๊ฐ€
26+
}
27+
const key = count.join('#'); // ์•ŒํŒŒ๋ฒณ ๊ฐœ์ˆ˜๋ฅผ ๋ฌธ์ž์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ํ‚ค ์ƒ์„ฑ
28+
if (!map.has(key)) {
29+
map.set(key, []); // ํ‚ค๊ฐ€ ์—†์œผ๋ฉด ์ƒˆ๋กœ์šด ๋ฐฐ์—ด ์ƒ์„ฑ
30+
}
31+
map.get(key).push(str); // ํ•ด๋‹น ํ‚ค์— ๋ฌธ์ž์—ด ์ถ”๊ฐ€
32+
}
33+
34+
return Array.from(map.values()); // Map์˜ ๊ฐ’๋“ค์„ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜ํ•˜์—ฌ ๋ฐ˜ํ™˜
35+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// TrieNode ํด๋ž˜์Šค ์ •์˜: ํŠธ๋ผ์ด์˜ ๊ฐ ๋…ธ๋“œ๋ฅผ ํ‘œํ˜„
2+
var TrieNode = function () {
3+
this.children = new Map(); // ์ž์‹ ๋…ธ๋“œ๋ฅผ Map์œผ๋กœ ์ €์žฅ
4+
this.isEnd = false; // ๋‹จ์–ด์˜ ๋์„ ํ‘œ์‹œํ•˜๋Š” ํ”Œ๋ž˜๊ทธ
5+
};
6+
7+
// Trie ํด๋ž˜์Šค ์ •์˜: ํŠธ๋ผ์ด ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ‘œํ˜„
8+
var Trie = function () {
9+
this.root = new TrieNode(); // ๋ฃจํŠธ ๋…ธ๋“œ
10+
};
11+
12+
/**
13+
* @param {string} word
14+
* @return {void}
15+
*/
16+
Trie.prototype.insert = function (word) {
17+
let node = this.root; // ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋ฃจํŠธ๋กœ ์ดˆ๊ธฐํ™”
18+
19+
for (let i = 0; i < word.length; i++) {
20+
const char = word[i]; // ํ˜„์žฌ ๋ฌธ์ž
21+
22+
// ํ˜„์žฌ ๋ฌธ์ž์— ๋Œ€ํ•œ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด ์ƒˆ๋กœ ์ƒ์„ฑ
23+
if (!node.children.has(char)) {
24+
node.children.set(char, new TrieNode());
25+
}
26+
27+
// ๋‹ค์Œ ์ž์‹ ๋…ธ๋“œ๋กœ ์ด๋™
28+
node = node.children.get(char);
29+
}
30+
node.isEnd = true; // ๋‹จ์–ด์˜ ๋์„ ํ‘œ์‹œ
31+
};
32+
33+
/**
34+
* @param {string} word
35+
* @return {boolean}
36+
*/
37+
Trie.prototype.search = function (word) {
38+
const node = this._searchPrefix(word); // ์ฃผ์–ด์ง„ ๋‹จ์–ด์˜ ๋…ธ๋“œ๋ฅผ ์ฐพ์Œ
39+
return node !== null && node.isEnd === true; // ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๊ณ , ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๊ฐ€ ๋‹จ์–ด์˜ ๋์ธ์ง€ ํ™•์ธ
40+
};
41+
42+
/**
43+
* @param {string} prefix
44+
* @return {boolean}
45+
*/
46+
Trie.prototype.startsWith = function (prefix) {
47+
return this._searchPrefix(prefix) !== null; // ๊ฒฝ๋กœ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€๋งŒ ํ™•์ธ
48+
};
49+
50+
/**
51+
* ํ—ฌํผ ๋ฉ”์„œ๋“œ: ์ฃผ์–ด์ง„ ์ ‘๋‘์‚ฌ์— ๋Œ€ํ•œ ๊ฒฝ๋กœ๋ฅผ ์ฐพ๋Š” ๋ฉ”์„œ๋“œ
52+
* @param {string} str
53+
* @return {TrieNode|null} ๊ฒฝ๋กœ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ ๋˜๋Š” null
54+
* @private
55+
*/
56+
Trie.prototype._searchPrefix = function (str) {
57+
let node = this.root; // ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋ฃจํŠธ๋กœ ์ดˆ๊ธฐํ™”
58+
59+
// ์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์˜ ๊ฐ ๋ฌธ์ž์— ๋Œ€ํ•ด ๋…ธ๋“œ ํƒ์ƒ‰
60+
for (let i = 0; i < str.length; i++) {
61+
const char = str[i]; // ํ˜„์žฌ ๋ฌธ์ž
62+
63+
// ํ˜„์žฌ ๋ฌธ์ž์— ๋Œ€ํ•œ ์ž์‹ ๋…ธ๋“œ๊ฐ€ ์—†์œผ๋ฉด null ๋ฆฌํ„ด
64+
if (!node.children.has(char)) {
65+
return null;
66+
}
67+
68+
// ๋‹ค์Œ ์ž์‹ ๋…ธ๋“œ๋กœ ์ด๋™
69+
node = node.children.get(char);
70+
}
71+
return node; // ๊ฒฝ๋กœ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ ๋ฐ˜ํ™˜
72+
};
73+
/**
74+
* Your Trie object will be instantiated and called as such:
75+
* var obj = new Trie()
76+
* obj.insert(word)
77+
* var param_2 = obj.search(word)
78+
* var param_3 = obj.startsWith(prefix)
79+
*/

โ€Žword-break/clara-shin.js

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* ๋ฌธ์ž์—ด s๊ฐ€ ์ฃผ์–ด์ง„ ๋‹จ์–ด ์‚ฌ์ „ wordDict์˜ ๋‹จ์–ด๋“ค๋กœ ๋ถ„๋ฆฌ๋  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ๋ฌธ์ œ
3+
* ๋‹ค์ด๋‚˜๋ฏน ํ”„๋กœ๊ทธ๋ž˜๋ฐ(DP)
4+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n * k) (k: ์ตœ๋Œ€ ๋‹จ์–ด ๊ธธ์ด)
5+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n) (DP ๋ฐฐ์—ด + ๋‹จ์–ด ์ง‘ํ•ฉ)
6+
*/
7+
8+
/**
9+
* @param {string} s
10+
* @param {string[]} wordDict
11+
* @return {boolean}
12+
*/
13+
var wordBreak = function (s, wordDict) {
14+
const n = s.length;
15+
const dp = new Array(n + 1).fill(false); // DP ๋ฐฐ์—ด ์ดˆ๊ธฐํ™”
16+
dp[0] = true; // ๋นˆ ๋ฌธ์ž์—ด์€ ํ•ญ์ƒ ๋ถ„๋ฆฌ ๊ฐ€๋Šฅ
17+
18+
const wordSet = new Set(wordDict); // ๋‹จ์–ด ์‚ฌ์ „์„ Set์œผ๋กœ ๋ณ€ํ™˜: ๊ฒ€์ƒ‰์‹œ๊ฐ„ O(1)
19+
20+
const maxWordLength = Math.max(...wordDict.map((word) => word.length)); // ๋‹จ์–ด์˜ ์ตœ๋Œ€ ๊ธธ์ด ์ฐพ๊ธฐ
21+
22+
for (let i = 0; i <= n; i++) {
23+
// ๊ฐ€๋Šฅํ•œ ๋‹จ์–ด ๊ธธ์ด๋งŒ ๊ฒ€์‚ฌ
24+
for (let j = Math.max(0, i - maxWordLength); j < i; j++) {
25+
if (dp[j] && wordSet.has(s.substring(j, i))) {
26+
// dp[j]๊ฐ€ true์ด๊ณ , j๋ถ€ํ„ฐ i๊นŒ์ง€์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ๋‹จ์–ด ์‚ฌ์ „์— ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ
27+
dp[i] = true;
28+
break;
29+
}
30+
}
31+
}
32+
33+
return dp[n];
34+
};

โ€Žword-search/clara-shin.js

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๊ฒฉ์ž(board)์—์„œ ํŠน์ • ๋‹จ์–ด(word)๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋Š” ํ•จ์ˆ˜
3+
*
4+
* ์ ‘๊ทผ ๋ฐฉ์‹:DFS(๊นŠ์ด ์šฐ์„  ํƒ์ƒ‰) + ๋ฐฑํŠธ๋ž˜ํ‚น
5+
* ์‹œ๊ฐ„ ๋ณต์žก๋„: O(m * n * 4^k) (m: ํ–‰, n: ์—ด, k: ๋‹จ์–ด ๊ธธ์ด)
6+
* ๊ณต๊ฐ„ ๋ณต์žก๋„: O(m * n) (์ตœ๋Œ€ ๊นŠ์ด m*n)
7+
*
8+
*/
9+
10+
/**
11+
* @param {character[][]} board
12+
* @param {string} word
13+
* @return {boolean}
14+
*/
15+
var exist = function (board, word) {
16+
const m = board.length;
17+
const n = board[0].length;
18+
19+
const directions = [
20+
[-1, 0],
21+
[1, 0],
22+
[0, -1],
23+
[0, 1],
24+
];
25+
26+
function dfs(row, col, index) {
27+
if (index === word.length) {
28+
return true;
29+
}
30+
31+
if (row < 0 || row >= m || col < 0 || col >= n || board[row][col] !== word[index]) {
32+
return false;
33+
}
34+
35+
// ํ˜„์žฌ ์…€ ๋ฐฉ๋ฌธ ํ‘œ์‹œ (์ž„์‹œ๋กœ ๋ณ€๊ฒฝ)
36+
const temp = board[row][col];
37+
board[row][col] = '#'; // ๋ฐฉ๋ฌธํ•œ ์…€์„ ํŠน์ˆ˜ ๋ฌธ์ž๋กœ ํ‘œ์‹œ
38+
39+
// ๋„ค ๋ฐฉํ–ฅ ํƒ์ƒ‰
40+
for (const [dx, dy] of directions) {
41+
const newRow = row + dx;
42+
const newCol = col + dy;
43+
44+
if (dfs(newRow, newCol, index + 1)) {
45+
// ๋‹จ์–ด๋ฅผ ์ฐพ์•˜์œผ๋ฉด ์›๋ž˜ ๊ฐ’ ๋กค๋ฐฑ ํ›„ true ๋ฐ˜ํ™˜
46+
board[row][col] = temp;
47+
return true;
48+
}
49+
}
50+
51+
// ๋ฐฑํŠธ๋ž˜ํ‚น(ํ˜„์žฌ ์…€์˜ ์›๋ž˜ ๊ฐ’ ๋กค๋ฐฑ)
52+
board[row][col] = temp;
53+
54+
return false;
55+
}
56+
57+
// ๊ฒฉ์ž์˜ ๋ชจ๋“  ์…€์—์„œ ์‹œ์ž‘์ ์œผ๋กœ ์‹œ๋„
58+
for (let i = 0; i < m; i++) {
59+
for (let j = 0; j < n; j++) {
60+
if (board[i][j] === word[0] && dfs(i, j, 0)) {
61+
return true;
62+
}
63+
}
64+
}
65+
66+
// ๋ชจ๋“  ์‹œ์ž‘์ ์—์„œ ์‹คํŒจํ•˜๋ฉด
67+
return false;
68+
};

0 commit comments

Comments
 (0)
Please sign in to comment.