Skip to content

Commit 75facce

Browse files
authored
Merge pull request #869 from YeomChaeeun/main
[YeomChaeeun] Week 5
2 parents 5ef7b5d + 7a9220a commit 75facce

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* ์ตœ๋Œ€ ์ˆ˜์ต์„ ๊ตฌํ•˜๋Š” ์•Œ๊ณ ๋ฆฌ์ฆ˜
3+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
4+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(n)
5+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(1)
6+
* @param prices
7+
*/
8+
function maxProfit(prices: number[]): number {
9+
let min = prices[0]
10+
let total = 0
11+
12+
for(let i = 1 ; i < prices.length ; i++) {
13+
min = Math.min(min, prices[i])
14+
// console.log(dp[i],'===', dp[i-1], '===', prices[i])
15+
total = Math.max(total, prices[i] - min)
16+
}
17+
18+
return total
19+
}

โ€Žgroup-anagrams/YeomChaeeun.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* ์• ๋„ˆ๊ทธ๋žจ ๊ทธ๋ฃนํ™”ํ•˜๊ธฐ
3+
* n - ์ž…๋ ฅ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์˜ ๊ธธ์ด
4+
* k - ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด์˜ ๊ธธ์ด
5+
*
6+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
7+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: n * k * logk (sort๊ฐ€ klogk ์‹œ๊ฐ„ ์†Œ์š”)
8+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: n * k
9+
* @param strs
10+
*/
11+
function groupAnagrams(strs: string[]): string[][] {
12+
let group = {}
13+
14+
for(const s of strs) {
15+
let key = s.split('').sort().join('');
16+
if(!group[key]) {
17+
group[key] = []
18+
}
19+
group[key].push(s)
20+
}
21+
// console.log(group)
22+
23+
return Object.values(group)
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* ํŠธ๋ผ์ด ์•Œ๊ณ ๋ฆฌ์ฆ˜
3+
* ๋‹ฌ๋ ˆ ํ•ด์„ค์„ ๋ณด๊ณ  ์ฐธ๊ณ ํ•˜์—ฌ TypeScript๋กœ ์ž‘์„ฑ
4+
*/
5+
class Node {
6+
children: { [key: string]: Node };
7+
ending: boolean;
8+
9+
constructor(ending: boolean = false) {
10+
this.children = {};
11+
this.ending = ending;
12+
}
13+
}
14+
15+
class Trie {
16+
private root: Node;
17+
18+
constructor() {
19+
this.root = new Node(true);
20+
}
21+
22+
insert(word: string): void {
23+
let node = this.root;
24+
for (const ch of word) {
25+
if (!(ch in node.children)) {
26+
node.children[ch] = new Node();
27+
}
28+
node = node.children[ch];
29+
}
30+
node.ending = true;
31+
}
32+
33+
search(word: string): boolean {
34+
let node = this.root;
35+
for (const ch of word) {
36+
if (!(ch in node.children)) {
37+
return false;
38+
}
39+
node = node.children[ch];
40+
}
41+
return node.ending;
42+
}
43+
44+
startsWith(prefix: string): boolean {
45+
let node = this.root;
46+
for (const ch of prefix) {
47+
if (!(ch in node.children)) {
48+
return false;
49+
}
50+
node = node.children[ch];
51+
}
52+
return true;
53+
}
54+
}

โ€Žword-break/YeomChaeeun.ts

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* ์ฃผ์–ด์ง„ ๋‹จ์–ด๊ฐ€ wordDict์— ์žˆ๋Š” ๋‹จ์–ด๋“ค๋กœ ๋ถ„ํ•  ๊ฐ€๋Šฅํ•œ์ง€ ํ™•์ธํ•˜๊ธฐ
3+
* ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ณต์žก๋„
4+
* - ์‹œ๊ฐ„ ๋ณต์žก๋„: O(nmk) => ๋ฌธ์ž์—ด s์˜ ๊ธธ์ด * wordDict ๋‹จ์–ด ๊ฐœ์ˆ˜ * wordDict ๋‚ด ๊ฐ€์žฅ ๊ธด ๋‹จ์–ด์˜ ๊ธธ์ด
5+
* - ๊ณต๊ฐ„ ๋ณต์žก๋„: O(n)
6+
* @param s
7+
* @param wordDict
8+
*/
9+
function wordBreak(s: string, wordDict: string[]): boolean {
10+
// ์ ‘๊ทผ 1 - ๋‹จ์–ด ํฌํ•จ ์—ฌ๋ถ€๋งŒ ๊ณ ๋ คํ•˜๊ณ  ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉ๋  ์žˆ๊ณ  ์—ฐ์†์„ฑ ๊ฒ€์‚ฌ๋ฅผ ํ•˜์ง€ ์•Š์•˜์Œ
11+
// let len = 0;
12+
// for(const word of wordDict) {
13+
// len += word.length
14+
// if(!s.includes(word)) return false
15+
// }
16+
// if(s.length < len) return false
17+
// return true
18+
19+
/* ํ•ด๋‹น ์ผ€์ด์Šค์—์„œ ์—๋Ÿฌ ๋ฐœ์ƒํ•จ
20+
* - ๋‹จ์–ด๊ฐ€ ์—ฌ๋Ÿฌ๋ฒˆ ์‚ฌ์šฉ๋  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ
21+
* s="bb"
22+
* wordDict = ["a","b","bbb","bbbb"]
23+
*/
24+
25+
// ์ ‘๊ทผ 2- dp ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ ์šฉ
26+
// ๊ฐ ์œ„์น˜๊นŒ์ง€์˜ ๋ฌธ์ž์—ด์ด ๋ถ„ํ•  ๊ฐ€๋Šฅํ•œ์ง€ ์ €์žฅํ•จ
27+
let dp: boolean[] = new Array(s.length).fill(false)
28+
29+
for (let i = 0; i < s.length; i++) {
30+
for (let word of wordDict) {
31+
// ์กฐ๊ฑด ์ฒ˜๋ฆฌ - ํ˜„์žฌ ์œ„์น˜๊ฐ€ ๋‹จ์–ด ๊ธธ์ด๋ณด๋‹ค ์ž‘์œผ๋ฉด ์Šคํ‚ตํ•จ
32+
if (i < word.length - 1) {
33+
continue
34+
}
35+
// ํ˜„์žฌ ์œ„์น˜์˜ ์œ ํšจ์„ฑ ์ฒดํฌ - ์ฒซ๋ฒˆ์งธ ๋‹จ์–ด์ด๊ฑฐ๋‚˜ ์ด์ „ ์œ„์น˜๊นŒ์ง€ ๋ถ„ํ•  ๊ฐ€๋Šฅํ•œ์ง€
36+
if (i == word.length - 1 || dp[i - word.length]) {
37+
// ํ˜„์žฌ ์œ„์น˜์˜ ๋‹จ์–ด ๋งค์นญ ์ฒดํฌ
38+
if (s.substring(i - word.length + 1, i + 1) == word) {
39+
dp[i] = true
40+
break
41+
}
42+
}
43+
}
44+
}
45+
return dp[s.length - 1]
46+
}

0 commit comments

Comments
ย (0)