-
Notifications
You must be signed in to change notification settings - Fork 125
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 #877 from Yjason-K/main
[gomgom22] Week5
- Loading branch information
Showing
4 changed files
with
129 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,28 @@ | ||
/** | ||
* ์ต๋ ์ด์ต์ ๊ณ์ฐํ๋ ํจ์ | ||
* @param {number[]} prices | ||
* @returns {number} | ||
* | ||
* ์๊ฐ ๋ณต์ก๋ : O(n) (n: ์ฃผ์ ๊ฐ๊ฒฉ ๋ฐฐ์ด์ ๊ธธ์ด) | ||
* ๊ณต๊ฐ ๋ณต์ก๋ : 0(1) (์ถ๊ฐ ์๋ฃ๊ตฌ์กฐ X) | ||
*/ | ||
function maxProfit(prices: number[]): number { | ||
let minPrice = 100001; // ์ง๊ธ๊น์ง์ ์ต์ ๊ฐ๊ฒฉ | ||
let maxProfit = 0; // ์ต๋ ์ด์ต | ||
|
||
for (let price of prices) { | ||
// ์ต์ ๊ฐ๊ฒฉ ๊ฐฑ์ | ||
if (price < minPrice) { | ||
minPrice = price; | ||
} | ||
|
||
// ํ์ฌ ๊ฐ๊ฒฉ์์ ์ต์ ๊ฐ๊ฒฉ์ ๋บ ์ด์ต์ด ์ต๋ ์ด์ต๋ณด๋ค ํฌ๋ค๋ฉด ๊ฐฑ์ | ||
const potentialProfit = price - minPrice; | ||
if (potentialProfit > maxProfit) { | ||
maxProfit = potentialProfit; | ||
} | ||
} | ||
|
||
return maxProfit; | ||
} | ||
|
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,28 @@ | ||
/** | ||
* @description ๋ฌธ์์ด ๋ฐฐ์ด์ ํ๋์ ๋ฌธ์์ด๋ก ์ธ์ฝ๋ฉํฉ๋๋ค. | ||
* @param {string[]} strs - ๋ฌธ์์ด ๋ฐฐ์ด | ||
* @returns {string} ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(N) | ||
* - N์ ์ ๋ ฅ ๋ฐฐ์ด์ ๋ชจ๋ ๋ฌธ์์ด ๊ธธ์ด์ ํฉ | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
* - ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์์ | ||
*/ | ||
function encode(strs: string[]): string { | ||
return strs.join(':'); | ||
} | ||
|
||
/** | ||
* @description ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด์ ๋ค์ ๋ฌธ์์ด ๋ฐฐ์ด๋ก ๋์ฝ๋ฉํฉ๋๋ค. | ||
* @param {string} s - ์ธ์ฝ๋ฉ๋ ๋ฌธ์์ด | ||
* @returns {string[]} ๋์ฝ๋ฉ๋ ๋ฌธ์์ด ๋ฐฐ์ด | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(N) | ||
* - N์ ์ ๋ ฅ ๋ฌธ์์ด์ ๊ธธ์ด | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
* - ์ถ๊ฐ ๋ฉ๋ชจ๋ฆฌ ์ฌ์ฉ ์์ | ||
*/ | ||
function decode(s: string): string[] { | ||
return s.split(':'); | ||
} | ||
|
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,34 @@ | ||
/** | ||
* ์ฃผ์ด์ง ๋ฌธ์์ด ๋ฐฐ์ด์์ Anagram์ ๊ทธ๋ฃนํ ํด์ ๋ฐฐ์ด ๋ง๋ค๊ธฐ | ||
* @param {string[]} strs - ๋ฌธ์์ด ๋ฐฐ์ด | ||
* @returns {string[][]} Anagram์ ๊ทธ๋ฃน ๋ฐฐ์ด | ||
* | ||
* ๋ฌธ์์ด๋ค์ ์ ๋ ฌํด์ Map์ ๋ด์์ ์กด์ฌํ๋ฉด ๊ทธ ๋์ ๋ฌธ์๋ฅผ value๋ก ์ถ๊ฐ | ||
* ์กด์ฌํ์ง ์์ผ๋ฉด ์๋ก์ด ๋ฐฐ์ด์ value๋ก ์ถ๊ฐ | ||
* | ||
* ์๊ฐ ๋ณต์ก๋: O(N * M * log(M)) | ||
* - N์ ๋ฌธ์์ด ๋ฐฐ์ด์ ๊ธธ์ด | ||
* - M์ ๊ฐ ๋ฌธ์์ด์ ํ๊ท ๊ธธ์ด (์ ๋ ฌ ์๊ฐ ๋๋ฌธ) | ||
* ๊ณต๊ฐ ๋ณต์ก๋: O(N * M) | ||
* - ํด์๋งต์ ์ ์ฅ๋๋ ๋ฌธ์์ด ๊ทธ๋ฃน ๋๋ฌธ | ||
*/ | ||
function groupAnagrams(strs: string[]): string[][] { | ||
const anagramMap: Map<string, string[]> = new Map(); | ||
for (const str of strs) { | ||
|
||
// ์ ๋ ฌ๋ ๋ฌธ์์ด | ||
const sortedStr = str.split('').sort().join(''); | ||
|
||
// ์ ๋ ฌ๋ ๋ฌธ์์ด์ด ์กด์ฌํ๋ ์๋ ๊ฒฝ์ฐ | ||
if (!anagramMap.has(sortedStr)) { | ||
anagramMap.set(sortedStr, []) | ||
} | ||
|
||
// ์ ๋ ฌ๋ ๋ฌธ์์ด์ key๋ก ํ๋ value str ์ถ๊ฐ | ||
anagramMap.get(sortedStr)?.push(str); | ||
} | ||
|
||
// anagramMap์์ values๋ง ๋ฐฐ์ด๋กํด์ ์ถ๋ ฅ | ||
return Array.from(anagramMap.values()) | ||
} | ||
|
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,39 @@ | ||
/** | ||
* ์ฃผ์ด์ง ๋ฌธ์์ด์ด ๋จ์ด ์ฌ์ ์ ์๋ ๋จ์ด๋ค๋ก ๋๋์ด์ง ์ ์๋์ง ํ์ธํฉ๋๋ค. | ||
* | ||
* | ||
* @param {string} s - ํ์ธํ ๋ฌธ์์ด | ||
* @param {string[]} wordDict - ๋จ์ด ์ฌ์ | ||
* @returns {boolean} ๋ฌธ์์ด์ด ๋จ์ด๋ก ์๋ฒฝํ ๋๋์ด์ง ์ ์์ผ๋ฉด `true`, ์๋๋ฉด `false` | ||
* | ||
* ์๊ฐ ๋ณต์ก์ฑ O(n * m * k) | ||
* - n: ๋ฌธ์์ด s ๊ธธ์ด | ||
* - m: ๋จ์ด ์ฌ์ ๊ธธ์ด | ||
* - k: ๋จ์ด ์ฌ์ ๋ด ๋จ์ด ๊ธธ์ด | ||
* | ||
* ๊ณต๊ฐ ๋ณต์ก์ฑ O(n) | ||
* - ๋ฉ๋ชจ์ด์ ์ด์ (memo) ๋ฐ ์ฌ๊ท ํธ์ถ ์คํ ํฌ๊ธฐ๊ฐ ๋ฌธ์์ด ๊ธธ์ด n์ ๋น๋ก. | ||
*/ | ||
function wordBreak(s: string, wordDict: string[]): boolean { | ||
const memo: Record<number, boolean> = {}; | ||
|
||
/** | ||
* @param {number} start - ํ์ฌ ์์ idx | ||
* @returns {boolean} ์ฃผ์ด์ง idx ๋ถํฐ ๋ฌธ์์ด์ ๋๋ ์ ์์ผ๋ฉด true | ||
*/ | ||
const dfs = (start: number): boolean => { | ||
if (start in memo) return memo[start]; | ||
if (start === s.length) return (memo[start] = true); | ||
|
||
for (const word of wordDict) { | ||
if (s.startsWith(word, start) && dfs(start + word.length)) { | ||
return (memo[start] = true); | ||
} | ||
} | ||
|
||
return (memo[start] = false); | ||
}; | ||
|
||
return dfs(0); | ||
} | ||
|