Skip to content

Commit

Permalink
Merge pull request #869 from YeomChaeeun/main
Browse files Browse the repository at this point in the history
[YeomChaeeun] Week 5
  • Loading branch information
YeomChaeeun authored Jan 11, 2025
2 parents 5ef7b5d + 7a9220a commit 75facce
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 0 deletions.
19 changes: 19 additions & 0 deletions best-time-to-buy-and-sell-stock/YeomChaeeun.ts
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
}
24 changes: 24 additions & 0 deletions group-anagrams/YeomChaeeun.ts
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)
}
54 changes: 54 additions & 0 deletions implement-trie-prefix-tree/YeomChaeeun.ts
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;
}
}
46 changes: 46 additions & 0 deletions word-break/YeomChaeeun.ts
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]
}

0 comments on commit 75facce

Please sign in to comment.