-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #852 from lledellebell/main
[dd_._._bb] Week 5
- Loading branch information
Showing
5 changed files
with
356 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* | ||
* @problem | ||
* ์ฃผ์ด์ง ์ฃผ์ ๊ฐ๊ฒฉ ๋ฐฐ์ด์์ ํ ๋ฒ์ ๋งค์์ ํ ๋ฒ์ ๋งค๋๋ฅผ ํตํด ์ป์ ์ ์๋ ์ต๋ ์ด์ต์ ๊ณ์ฐํฉ๋๋ค. | ||
* ๋งค์๋ ๋งค๋๋ณด๋ค ๋ฐ๋์ ๋จผ์ ์ด๋ฃจ์ด์ ธ์ผ ํฉ๋๋ค. | ||
* | ||
* @constraints | ||
* - 1 <= prices.length <= 105 | ||
* - 0 <= prices[i] <= 104 | ||
* | ||
* @param {number[]} prices - ๊ฐ ๋ ์ง๋ณ ์ฃผ์ ๊ฐ๊ฒฉ์ ๋ํ๋ด๋ ๋ฐฐ์ด | ||
* @returns {number} ์ต๋ ์ด์ต (์ด์ต์ ๋ผ ์ ์๋ ๊ฒฝ์ฐ 0 ๋ฐํ) | ||
* | ||
* @example | ||
* maxProfit([7, 1, 5, 3, 6, 4]); // 5 (2์ผ์ฐจ์ ๋งค์ํ๊ณ 5์ผ์ฐจ์ ๋งค๋) | ||
* maxProfit([7, 6, 4, 3, 1]); // 0 (์ด์ต์ ๋ผ ์ ์์) | ||
* | ||
* @complexity | ||
* - ์๊ฐ ๋ณต์ก๋: O(n) | ||
* ๋ฐฐ์ด์ ํ ๋ฒ ์ํํ๋ฉฐ ๊ฐ ์์์ ๋ํด ์์ ์๊ฐ ์ฐ์ฐ๋ง ์ํํฉ๋๋ค. | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
* ์ถ๊ฐ์ ์ธ ๋ฐฐ์ด์ด๋ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ์ฌ์ฉํ์ง ์๊ณ , ๋ ๊ฐ์ ๋ณ์๋ง ์ฌ์ฉํฉ๋๋ค. | ||
*/ | ||
function maxProfit(prices) { | ||
let min_price = Infinity; // ํ์ฌ๊น์ง์ ์ต์ ๋งค์ ๊ฐ๊ฒฉ (์ด๊ธฐ๊ฐ์ ๋ฌดํ๋) | ||
let max_profit = 0; // ํ์ฌ๊น์ง์ ์ต๋ ์ด์ต (์ด๊ธฐ๊ฐ์ 0) | ||
|
||
// ๋ฐฐ์ด์ ์ํํ๋ฉฐ ์ต์ ๋งค์ ๊ฐ๊ฒฉ๊ณผ ์ต๋ ์ด์ต์ ๊ณ์ฐ | ||
for (let price of prices) { | ||
if (price < min_price) { | ||
// ํ์ฌ ์ฃผ์ ๊ฐ๊ฒฉ์ด ์ต์ ๋งค์ ๊ฐ๊ฒฉ๋ณด๋ค ์์ผ๋ฉด ์ต์ ๋งค์ ๊ฐ๊ฒฉ ๊ฐฑ์ | ||
min_price = price; | ||
} else if (price - min_price > max_profit) { | ||
// ํ์ฌ ์ฃผ์ ๊ฐ๊ฒฉ์์ ์ต์ ๋งค์ ๊ฐ๊ฒฉ์ ๋บ ๊ฐ(ํ์ฌ ์ด์ต)์ด ์ต๋ ์ด์ต๋ณด๋ค ํฌ๋ฉด ์ต๋ ์ด์ต ๊ฐฑ์ | ||
max_profit = price - min_price; | ||
} | ||
} | ||
|
||
return max_profit; // ์ต๋ ์ด์ต ๋ฐํ | ||
} | ||
|
||
console.log(maxProfit([7, 1, 5, 3, 6, 4])); // 5 | ||
console.log(maxProfit([7, 6, 4, 3, 1])); // 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/** | ||
* | ||
* @problem | ||
* ๋ฌธ์์ด ๋ฐฐ์ด์ ๋จ์ผ ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํ๊ณ , | ||
* ๋ค์ ์๋์ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋์ฝ๋ฉํ๋ ๊ธฐ๋ฅ์ ๋ง๋ค์ด์ผ ํฉ๋๋ค. | ||
* | ||
* @example | ||
* const encoded = encode(["hello", "world"]); | ||
* console.log(encoded); // "5?hello5?world" | ||
* const decoded = decode(encoded); | ||
* console.log(decoded); // ["hello", "world"] | ||
* | ||
* @complexity | ||
* - ์๊ฐ ๋ณต์ก๋: | ||
* ใด encode: O(n) (n์ ๋ฌธ์์ด ๋ฐฐ์ด์ ์ด ๊ธธ์ด) | ||
* ใด decode: O(n) (n์ ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด์ ๊ธธ์ด) | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: | ||
* ใด encode: O(1) (์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์์) | ||
* ใด decode: O(1) (๊ฒฐ๊ณผ ๋ฐฐ์ด์ ์ ์ธํ ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์์) | ||
*/ | ||
|
||
/** | ||
* @param {string[]} strs - ์ธ์ฝ๋ฉํ ๋ฌธ์์ด ๋ฐฐ์ด | ||
* @returns {string} - ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด | ||
*/ | ||
const encode = (strs) => { | ||
let encoded = ''; | ||
for (const str of strs) { | ||
// ๋ฌธ์์ด์ "๊ธธ์ด?๋ฌธ์์ด" ํ์์ผ๋ก ์ถ๊ฐ | ||
encoded += `${str.length}?${str}`; | ||
} | ||
return encoded; | ||
}; | ||
|
||
/** | ||
* @param {string} s - ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด | ||
* @returns {string[]} - ๋์ฝ๋ฉ๋ ๋ฌธ์์ด ๋ฐฐ์ด | ||
*/ | ||
const decode = (s) => { | ||
const result = []; | ||
let i = 0; | ||
|
||
while (i < s.length) { | ||
// ํ์ฌ ์์น์์ ์ซ์(๊ธธ์ด)๋ฅผ ์ฝ์ | ||
let length = 0; | ||
while (s[i] !== '?') { | ||
length = length * 10 + (s[i].charCodeAt(0) - '0'.charCodeAt(0)); // ์ซ์ ๊ณ์ฐ | ||
i++; | ||
} | ||
|
||
// '?' ์ดํ์ ๋ฌธ์์ด์ ์ถ์ถ | ||
i++; // '?'๋ฅผ ๊ฑด๋๋ | ||
const str = s.substring(i, i + length); | ||
result.push(str); | ||
|
||
// ๋ค์ ๋ฌธ์์ด๋ก ์ด๋ | ||
i += length; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
const encoded = encode(["hello", "world"]); | ||
console.log(encoded); // "5?hello5?world" | ||
|
||
const decoded = decode(encoded); | ||
console.log(decoded); // ["hello", "world"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* | ||
* @problem | ||
* ๋ฌธ์์ด ๋ฐฐ์ด์ด ์ฃผ์ด์ก์ ๋, ์ ๋๊ทธ๋จ๋ผ๋ฆฌ ๊ทธ๋ฃนํํด์ผ ํฉ๋๋ค. | ||
* | ||
* (์ฐธ๊ณ ) | ||
* ์ ๋๊ทธ๋จ(Anagram)์ด๋ ๋จ์ด๋ฅผ ๊ตฌ์ฑํ๋ ๋ฌธ์์ ์์๋ฅผ ๋ฐ๊ฟ์ ๋ค๋ฅธ ๋จ์ด๋ฅผ ๋ง๋๋ ๊ฒ์ ์๋ฏธํฉ๋๋ค. | ||
* ์๋ฅผ ๋ค์ด, "eat", "tea", "ate"๋ ๋ชจ๋ ๊ฐ์ ๋ฌธ์๋ก ๊ตฌ์ฑ๋์ด ์์ผ๋ฏ๋ก ์ ๋๊ทธ๋จ์ ๋๋ค. | ||
* | ||
* @constraints | ||
* - 1 <= strs.length <= 104 | ||
* - 0 <= strs[i].length <= 100 | ||
* - strs[i]๋ ์๋ฌธ์ ์ํ๋ฒณ์ผ๋ก๋ง ๊ตฌ์ฑ๋ฉ๋๋ค. | ||
* | ||
* @param {string[]} strs - ์ ๋ ฅ ๋ฌธ์์ด ๋ฐฐ์ด | ||
* @returns {string[][]} ์ ๋๊ทธ๋จ ๊ทธ๋ฃน์ผ๋ก ๋ฌถ์ธ 2์ฐจ์ ๋ฌธ์์ด ๋ฐฐ์ด | ||
* | ||
* @example | ||
* groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"]); // [["bat"], ["nat", "tan"], ["ate", "eat", "tea"]] | ||
* groupAnagrams([""]); // [[""]] | ||
* groupAnagrams(["a"]); // [["a"]] | ||
* | ||
* @description | ||
* - ๊ฐ ๋ฌธ์์ด์ ์ ๋ ฌํ์ฌ ๋์ผํ ๋ฌธ์๋ฅผ ๊ฐ์ง ๋ฌธ์์ด๋ค์ ๊ทธ๋ฃนํํฉ๋๋ค. | ||
* - ์ ๋ ฌ๋ ๋ฌธ์์ด์ ํค๋ก ์ฌ์ฉํ์ฌ ํด์๋งต(Map)์ ์ ์ฅํฉ๋๋ค. | ||
* - ์ต์ข ์ ์ผ๋ก ํด์๋งต์ ๊ฐ๋ค๋ง ์ถ์ถํ์ฌ ๋ฐํํฉ๋๋ค. | ||
* | ||
* @complexity | ||
* - ์๊ฐ ๋ณต์ก๋: O(N * K log K) | ||
* ใด N: ์ ๋ ฅ ๋ฌธ์์ด ๋ฐฐ์ด์ ๊ธธ์ด | ||
* ใด K: ๊ฐ ๋ฌธ์์ด์ ํ๊ท ๊ธธ์ด | ||
* ๊ฐ ๋ฌธ์์ด์ ์ ๋ ฌํ๋ ๋ฐ O(K log K)์ ์๊ฐ์ด ์์๋๋ฉฐ, ์ด๋ฅผ N๋ฒ ๋ฐ๋ณตํฉ๋๋ค. | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: O(N * K) | ||
* ํด์๋งต์ ์ ์ฅ๋๋ ํค์ ๊ฐ์ ์ด ๊ธธ์ด์ ๋น๋กํฉ๋๋ค. | ||
*/ | ||
function groupAnagrams(strs: string[]): string[][] { | ||
const anagrams = new Map<string, string[]>(); | ||
|
||
for (const str of strs) { | ||
const key = str.split('').sort().join(''); | ||
if (!anagrams.has(key)) { | ||
anagrams.set(key, []); | ||
} | ||
anagrams.get(key)!.push(str); | ||
} | ||
|
||
return Array.from(anagrams.values()); | ||
} | ||
|
||
console.log(groupAnagrams(["eat", "tea", "tan", "ate", "nat", "bat"])); // [["bat"], ["nat", "tan"], ["ate", "eat", "tea"]] | ||
console.log(groupAnagrams([""])); // [[""]] | ||
console.log(groupAnagrams(["a"])); // [["a"]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
/** | ||
* @problem | ||
* Trie๋ฅผ ๊ตฌํํฉ๋๋ค. | ||
* - ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ๋์ ์ต์ ํํ๋ฉด์ ์ ํํ ๊ฒ์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฅํด์ผ ํฉ๋๋ค. | ||
* | ||
* @constraints | ||
* - word์ prefix์ ๊ธธ์ด๋ ์ต์ 1, ์ต๋ 2000์ ๋๋ค. | ||
* - word์ prefix๋ ์๋ฌธ์ ์์ด ์ํ๋ฒณ(a-z)๋ง์ผ๋ก ๊ตฌ์ฑ๋ฉ๋๋ค. | ||
* - insert, search, startsWith ํจ์ ํธ์ถ์ ์ด 30,000๋ฒ์ ๋์ง ์์ต๋๋ค. | ||
* | ||
* @example | ||
* const trie = new Trie(); | ||
* trie.insert("apple"); // undefined | ||
* trie.search("apple"); // true | ||
* trie.search("app"); // false | ||
* trie.startsWith("app"); // true | ||
* trie.insert("app"); // undefined | ||
* trie.search("app"); // true | ||
* | ||
* @complexity | ||
* - ์๊ฐ๋ณต์ก๋: | ||
* ใด insert: O(m) (m์ ๋จ์ด ๊ธธ์ด) | ||
* ใด search: O(m) (m์ ๋จ์ด ๊ธธ์ด) | ||
* ใด startsWith: O(m) (m์ ์ ๋์ฌ ๊ธธ์ด) | ||
* - ๊ณต๊ฐ๋ณต์ก๋: O(ALPHABET_SIZE * m * n) | ||
* ใด ALPHABET_SIZE: ๋ฌธ์์ด์ ์ํ๋ฒณ ์ (์๋ฌธ์ ๊ฒฝ์ฐ 26) | ||
* ใด m: ๋จ์ด์ ํ๊ท ๊ธธ์ด | ||
*/ | ||
class TrieNode { | ||
constructor() { | ||
// ๊ฐ ๋ฌธ์๋ฅผ ํค๋ก ํ๊ณ ์์ ๋ ธ๋๋ฅผ ๊ฐ์ผ๋ก ํ๋ ๊ฐ์ฒด | ||
this.children = {}; | ||
// ํ์ฌ ๋ ธ๋๊ฐ ๋จ์ด์ ๋ง์ง๋ง ๋ฌธ์์ธ์ง ํ์ํ๋ ํ๋๊ทธ | ||
this.isEndOfWord = false; | ||
} | ||
} | ||
|
||
class Trie { | ||
constructor() { | ||
// ๋น ๋ฌธ์์ด์ ๋ํ๋ด๋ ๋ฃจํธ ๋ ธ๋ ์์ฑ | ||
this.root = new TrieNode(); | ||
} | ||
|
||
/** | ||
* ๋จ์ด๋ฅผ Trie์ ์ฝ์ ํ๋ ๋ฉ์๋ | ||
* @param {string} word - ์ฝ์ ํ ๋จ์ด | ||
*/ | ||
insert(word) { | ||
// ๋ฃจํธ ๋ ธ๋๋ถํฐ ์์ | ||
let node = this.root; | ||
|
||
// ๋จ์ด์ ๊ฐ ๋ฌธ์๋ฅผ ์ํ | ||
for (let i = 0; i < word.length; i++) { | ||
const char = word[i]; | ||
// ํ์ฌ ๋ฌธ์์ ๋ํ ๋ ธ๋๊ฐ ์์ผ๋ฉด ์๋ก ์์ฑ | ||
if (!(char in node.children)) { | ||
node.children[char] = new TrieNode(); | ||
} | ||
// ๋ค์ ๋ฌธ์๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์์ ๋ ธ๋๋ก ์ด๋ | ||
node = node.children[char]; | ||
} | ||
// ๋จ์ด์ ๋ง์ง๋ง ๋ฌธ์์์ ํ์ | ||
node.isEndOfWord = true; | ||
} | ||
|
||
|
||
/** | ||
* ์ ํํ ๋จ์ด๊ฐ ์กด์ฌํ๋์ง ๊ฒ์ํ๋ ๋ฉ์๋ | ||
* @param {string} word - ๊ฒ์ํ ๋จ์ด | ||
* @returns {boolean} - ๋จ์ด ์กด์ฌ ์ฌ๋ถ | ||
*/ | ||
search(word) { | ||
// ๋จ์ด๋ฅผ ์ฐพ์ ๋ง์ง๋ง ๋ ธ๋๋ฅผ ๋ฐํ | ||
const node = this._traverse(word); | ||
// ๋จ์ด๊ฐ ์กด์ฌํ๊ณ ํด๋น ๋ ธ๋๊ฐ ๋จ์ด์ ๋์ธ ๊ฒฝ์ฐ์๋ง true ๋ฐํ | ||
return node !== null && node.isEndOfWord; | ||
} | ||
|
||
/** | ||
* ์ฃผ์ด์ง ์ ๋์ฌ๋ก ์์ํ๋ ๋จ์ด๊ฐ ์กด์ฌํ๋์ง ๊ฒ์ํ๋ ๋ฉ์๋ | ||
* @param {string} prefix - ๊ฒ์ํ ์ ๋์ฌ | ||
* @returns {boolean} - ์ ๋์ฌ๋ก ์์ํ๋ ๋จ์ด ์กด์ฌ ์ฌ๋ถ | ||
*/ | ||
startsWith(prefix) { | ||
// ์ ๋์ฌ์ ํด๋นํ๋ ๋ ธ๋๊ฐ ์กด์ฌํ๋์ง๋ง ํ์ธ | ||
return this._traverse(prefix) !== null; | ||
} | ||
|
||
/** | ||
* ๋ฌธ์์ด์ ๋ฐ๋ผ๊ฐ๋ฉฐ ๋ง์ง๋ง ๋ ธ๋๋ฅผ ๋ฐํํ๋ ๋ด๋ถ ํฌํผ ๋ฉ์๋ | ||
* @param {string} str - ํ์ํ ๋ฌธ์์ด | ||
* @returns {TrieNode|null} - ๋ง์ง๋ง ๋ฌธ์์ ํด๋นํ๋ ๋ ธ๋ ๋๋ null | ||
* @private | ||
*/ | ||
_traverse(str) { | ||
// ๋ฃจํธ ๋ ธ๋๋ถํฐ ์์ | ||
let node = this.root; | ||
|
||
// ๋ฌธ์์ด์ ๊ฐ ๋ฌธ์๋ฅผ ์ํ | ||
for (let i = 0; i < str.length; i++) { | ||
const char = str[i]; | ||
// ํ์ฌ ๋ฌธ์์ ๋ํ ๋ ธ๋๊ฐ ์์ผ๋ฉด null ๋ฐํ | ||
if (!(char in node.children)) { | ||
return null; | ||
} | ||
// ๋ค์ ๋ฌธ์๋ฅผ ์ฒ๋ฆฌํ๊ธฐ ์ํด ์์ ๋ ธ๋๋ก ์ด๋ | ||
node = node.children[char]; | ||
} | ||
// ๋ง์ง๋ง ๋ ธ๋ ๋ฐํ | ||
return node; | ||
} | ||
} | ||
|
||
const trie = new Trie(); | ||
console.log(trie.insert("apple")); // undefined | ||
console.log(trie.search("apple")); // true | ||
console.log(trie.search("app")); // false | ||
console.log(trie.startsWith("app")); // true | ||
console.log(trie.insert("app")); // undefined | ||
console.log(trie.search("app")); // true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* @problem | ||
* ์ฃผ์ด์ง ๋ฌธ์์ด s๊ฐ ๋จ์ด ์ฌ์ wordDict์ ํฌํจ๋ ๋จ์ด๋ค๋ก๋ง ๊ตฌ์ฑ๋ ์ ์๋์ง ํ์ธํ๋ ๋ฌธ์ ์ ๋๋ค. | ||
* ๋จ์ด ์ฌ์ ์ ๋จ์ด๋ค์ ์ฌ๋ฌ ๋ฒ ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ๋ฌธ์์ด s๋ฅผ ์์ ํ ๋๋ ์ ์์ด์ผ ํฉ๋๋ค. | ||
* | ||
* @constraints | ||
* - 1 <= s.length <= 300 | ||
* - 1 <= wordDict.length <= 1000 | ||
* - 1 <= wordDict[i].length <= 20 | ||
* - s์ wordDict[i]๋ ๋ชจ๋ ์๋ฌธ์ ์ํ๋ฒณ์ผ๋ก๋ง ๊ตฌ์ฑ๋ฉ๋๋ค. | ||
* | ||
* @param {string} s - ์ฃผ์ด์ง ๋ฌธ์์ด | ||
* @param {string[]} wordDict - ๋จ์ด ์ฌ์ | ||
* @returns {boolean} ๋ฌธ์์ด s๊ฐ ๋จ์ด ์ฌ์ ์ ๋จ์ด๋ค๋ก๋ง ๋๋ ์ ์๋์ง ์ฌ๋ถ | ||
* | ||
* @example | ||
* - ์์ 1: | ||
* ใด Input: s = "leetcode", wordDict = ["leet", "code"] | ||
* ใด Output: true | ||
* ใด Explanation: "leetcode"๋ "leet" + "code"๋ก ๋๋ ์ ์์ต๋๋ค. | ||
* - ์์ 2: | ||
* ใด Input: s = "applepenapple", wordDict = ["apple", "pen"] | ||
* ใด Output: true | ||
* ใด Explanation: "applepenapple"๋ "apple" + "pen" + "apple"๋ก ๋๋ ์ ์์ต๋๋ค. | ||
* - ์์ 3: | ||
* ใด Input: s = "catsandog", wordDict = ["cats", "dog", "sand", "and", "cat"] | ||
* ใด Output: false | ||
* ใด Explanation: "catsandog"๋ wordDict์ ๋จ์ด๋ค๋ก ๋๋ ์ ์์ต๋๋ค. | ||
* | ||
* @complexity | ||
* - ์๊ฐ ๋ณต์ก๋: O(n^2) | ||
* ใด ์ธ๋ถ ๋ฐ๋ณต๋ฌธ: ๋ฌธ์์ด s์ ๊ธธ์ด n์ ๋ํด 1๋ถํฐ n๊น์ง ๋ฐ๋ณต (O(n)) | ||
* ใด ๋ด๋ถ ๋ฐ๋ณต๋ฌธ: ๊ฐ i์ ๋ํด ์ต๋ i๋ฒ ๋ฐ๋ณต (O(n)) | ||
* ใด substring ๋ฐ Set ๊ฒ์: O(1) (substring์ ๋ด๋ถ์ ์ผ๋ก O(k)์ด์ง๋ง, k๋ ์ต๋ ๋จ์ด ๊ธธ์ด๋ก ๊ฐ์ฃผ) | ||
* ใด ๊ฒฐ๊ณผ์ ์ผ๋ก O(n^2)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: O(n + m) | ||
* ใด dp ๋ฐฐ์ด์ ํฌ๊ธฐ: s์ ๊ธธ์ด n + 1 (O(n)) | ||
* ใด wordSet: wordDict์ ๋จ์ด ๊ฐ์์ ๋น๋ก (O(m), m์ wordDict์ ๋จ์ด ์) | ||
*/ | ||
function wordBreak(s, wordDict) { | ||
// wordDict๋ฅผ Set์ผ๋ก ๋ณํํ์ฌ ๊ฒ์ ์๋๋ฅผ O(1)๋ก ๋ง๋ฆ | ||
const wordSet = new Set(wordDict); | ||
|
||
// dp ๋ฐฐ์ด ์์ฑ: dp[i]๋ s์ ์ฒ์๋ถํฐ i๋ฒ์งธ ๋ฌธ์๊น์ง๊ฐ wordDict์ ๋จ์ด๋ค๋ก ๋๋ ์ ์๋์ง๋ฅผ ๋ํ๋ | ||
const dp = new Array(s.length + 1).fill(false); | ||
dp[0] = true; // ๋น ๋ฌธ์์ด์ ํญ์ ๋๋ ์ ์์ | ||
|
||
// i๋ ๋ฌธ์์ด์ ๋ ์ธ๋ฑ์ค๋ฅผ ๋ํ๋ | ||
for (let i = 1; i <= s.length; i++) { | ||
// j๋ ๋ฌธ์์ด์ ์์ ์ธ๋ฑ์ค๋ฅผ ๋ํ๋ | ||
for (let j = 0; j < i; j++) { | ||
// dp[j]๊ฐ true์ด๊ณ , s[j:i]๊ฐ wordSet์ ํฌํจ๋์ด ์๋ค๋ฉด | ||
if (dp[j] && wordSet.has(s.substring(j, i))) { | ||
dp[i] = true; // dp[i]๋ฅผ true๋ก ์ค์ | ||
break; // ๋ ์ด์ ํ์ธํ ํ์ ์์ | ||
} | ||
} | ||
} | ||
|
||
// dp[s.length]๊ฐ true๋ผ๋ฉด ๋ฌธ์์ด s๋ฅผ wordDict์ ๋จ์ด๋ค๋ก ๋๋ ์ ์์ | ||
return dp[s.length]; | ||
} | ||
|
||
const s1 = "leetcode"; | ||
const wordDict1 = ["leet", "code"]; | ||
console.log(wordBreak(s1, wordDict1)); // true | ||
|
||
const s2 = "applepenapple"; | ||
const wordDict2 = ["apple", "pen"]; | ||
console.log(wordBreak(s2, wordDict2)); // true | ||
|
||
const s3 = "catsandog"; | ||
const wordDict3 = ["cats", "dog", "sand", "and", "cat"]; | ||
console.log(wordBreak(s3, wordDict3)); // false |