-
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 #869 from YeomChaeeun/main
[YeomChaeeun] Week 5
- Loading branch information
Showing
4 changed files
with
143 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,19 @@ | ||
/** | ||
* ์ต๋ ์์ต์ ๊ตฌํ๋ ์๊ณ ๋ฆฌ์ฆ | ||
* ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋ | ||
* - ์๊ฐ ๋ณต์ก๋: O(n) | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
* @param prices | ||
*/ | ||
function maxProfit(prices: number[]): number { | ||
let min = prices[0] | ||
let total = 0 | ||
|
||
for(let i = 1 ; i < prices.length ; i++) { | ||
min = Math.min(min, prices[i]) | ||
// console.log(dp[i],'===', dp[i-1], '===', prices[i]) | ||
total = Math.max(total, prices[i] - min) | ||
} | ||
|
||
return total | ||
} |
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,24 @@ | ||
/** | ||
* ์ ๋๊ทธ๋จ ๊ทธ๋ฃนํํ๊ธฐ | ||
* n - ์ ๋ ฅ ๋ฌธ์์ด ๋ฐฐ์ด์ ๊ธธ์ด | ||
* k - ๊ฐ์ฅ ๊ธด ๋ฌธ์์ด์ ๊ธธ์ด | ||
* | ||
* ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋ | ||
* - ์๊ฐ ๋ณต์ก๋: n * k * logk (sort๊ฐ klogk ์๊ฐ ์์) | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: n * k | ||
* @param strs | ||
*/ | ||
function groupAnagrams(strs: string[]): string[][] { | ||
let group = {} | ||
|
||
for(const s of strs) { | ||
let key = s.split('').sort().join(''); | ||
if(!group[key]) { | ||
group[key] = [] | ||
} | ||
group[key].push(s) | ||
} | ||
// console.log(group) | ||
|
||
return Object.values(group) | ||
} |
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,54 @@ | ||
/** | ||
* ํธ๋ผ์ด ์๊ณ ๋ฆฌ์ฆ | ||
* ๋ฌ๋ ํด์ค์ ๋ณด๊ณ ์ฐธ๊ณ ํ์ฌ TypeScript๋ก ์์ฑ | ||
*/ | ||
class Node { | ||
children: { [key: string]: Node }; | ||
ending: boolean; | ||
|
||
constructor(ending: boolean = false) { | ||
this.children = {}; | ||
this.ending = ending; | ||
} | ||
} | ||
|
||
class Trie { | ||
private root: Node; | ||
|
||
constructor() { | ||
this.root = new Node(true); | ||
} | ||
|
||
insert(word: string): void { | ||
let node = this.root; | ||
for (const ch of word) { | ||
if (!(ch in node.children)) { | ||
node.children[ch] = new Node(); | ||
} | ||
node = node.children[ch]; | ||
} | ||
node.ending = true; | ||
} | ||
|
||
search(word: string): boolean { | ||
let node = this.root; | ||
for (const ch of word) { | ||
if (!(ch in node.children)) { | ||
return false; | ||
} | ||
node = node.children[ch]; | ||
} | ||
return node.ending; | ||
} | ||
|
||
startsWith(prefix: string): boolean { | ||
let node = this.root; | ||
for (const ch of prefix) { | ||
if (!(ch in node.children)) { | ||
return false; | ||
} | ||
node = node.children[ch]; | ||
} | ||
return 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,46 @@ | ||
/** | ||
* ์ฃผ์ด์ง ๋จ์ด๊ฐ wordDict์ ์๋ ๋จ์ด๋ค๋ก ๋ถํ ๊ฐ๋ฅํ์ง ํ์ธํ๊ธฐ | ||
* ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋ | ||
* - ์๊ฐ ๋ณต์ก๋: O(nmk) => ๋ฌธ์์ด s์ ๊ธธ์ด * wordDict ๋จ์ด ๊ฐ์ * wordDict ๋ด ๊ฐ์ฅ ๊ธด ๋จ์ด์ ๊ธธ์ด | ||
* - ๊ณต๊ฐ ๋ณต์ก๋: O(n) | ||
* @param s | ||
* @param wordDict | ||
*/ | ||
function wordBreak(s: string, wordDict: string[]): boolean { | ||
// ์ ๊ทผ 1 - ๋จ์ด ํฌํจ ์ฌ๋ถ๋ง ๊ณ ๋ คํ๊ณ ์ฌ๋ฌ๋ฒ ์ฌ์ฉ๋ ์๊ณ ์ฐ์์ฑ ๊ฒ์ฌ๋ฅผ ํ์ง ์์์ | ||
// let len = 0; | ||
// for(const word of wordDict) { | ||
// len += word.length | ||
// if(!s.includes(word)) return false | ||
// } | ||
// if(s.length < len) return false | ||
// return true | ||
|
||
/* ํด๋น ์ผ์ด์ค์์ ์๋ฌ ๋ฐ์ํจ | ||
* - ๋จ์ด๊ฐ ์ฌ๋ฌ๋ฒ ์ฌ์ฉ๋ ์ ์๊ธฐ ๋๋ฌธ | ||
* s="bb" | ||
* wordDict = ["a","b","bbb","bbbb"] | ||
*/ | ||
|
||
// ์ ๊ทผ 2- dp ์๊ณ ๋ฆฌ์ฆ ์ ์ฉ | ||
// ๊ฐ ์์น๊น์ง์ ๋ฌธ์์ด์ด ๋ถํ ๊ฐ๋ฅํ์ง ์ ์ฅํจ | ||
let dp: boolean[] = new Array(s.length).fill(false) | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
for (let word of wordDict) { | ||
// ์กฐ๊ฑด ์ฒ๋ฆฌ - ํ์ฌ ์์น๊ฐ ๋จ์ด ๊ธธ์ด๋ณด๋ค ์์ผ๋ฉด ์คํตํจ | ||
if (i < word.length - 1) { | ||
continue | ||
} | ||
// ํ์ฌ ์์น์ ์ ํจ์ฑ ์ฒดํฌ - ์ฒซ๋ฒ์งธ ๋จ์ด์ด๊ฑฐ๋ ์ด์ ์์น๊น์ง ๋ถํ ๊ฐ๋ฅํ์ง | ||
if (i == word.length - 1 || dp[i - word.length]) { | ||
// ํ์ฌ ์์น์ ๋จ์ด ๋งค์นญ ์ฒดํฌ | ||
if (s.substring(i - word.length + 1, i + 1) == word) { | ||
dp[i] = true | ||
break | ||
} | ||
} | ||
} | ||
} | ||
return dp[s.length - 1] | ||
} |