Skip to content

Commit ac71dd2

Browse files
authored
Merge pull request #1396 from hoyeongkwak/main
[hoyeongkwak] Week 05 Solutions
2 parents 74b1831 + 2b7256f commit ac71dd2

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(1)
4+
*/
5+
function maxProfit(prices: number[]): number {
6+
let left = 0
7+
let right = 0
8+
let maxProfit = 0
9+
10+
while (right < prices.length) {
11+
const curProfit = prices[right] - prices[left]
12+
if (prices[left] < prices[right]) {
13+
maxProfit = Math.max(curProfit, maxProfit)
14+
} else {
15+
left = right
16+
}
17+
right += 1
18+
}
19+
return maxProfit
20+
};

group-anagrams/hoyeongkwak.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
time complexity : O(n * mlogm)
3+
space complexity : O(n * m)
4+
*/
5+
function groupAnagrams(strs: string[]): string[][] {
6+
const strMap = new Map<string, string[]>()
7+
for (let i = 0; i < strs.length; i++) {
8+
const sortedStr = strs[i].split('').sort().join('')
9+
if (!strMap.has(sortedStr)) {
10+
strMap.set(sortedStr, [strs[i]])
11+
} else {
12+
const prevArr = strMap.get(sortedStr)
13+
prevArr.push(strs[i])
14+
strMap.set(sortedStr, prevArr)
15+
}
16+
}
17+
return Array.from(strMap.values())
18+
};
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
time complexity : O(n)
3+
space complexity : O(n)
4+
*/
5+
class TriedNode {
6+
children: Map<string, TriedNode>
7+
isEnd: boolean
8+
constructor(){
9+
this.children = new Map()
10+
this.isEnd = false
11+
}
12+
}
13+
14+
class Trie {
15+
root: TriedNode
16+
17+
constructor() {
18+
this.root = new TriedNode()
19+
}
20+
21+
insert(word: string): void {
22+
let node = this.root
23+
for (const char of word) {
24+
if (!node.children.has(char)) {
25+
node.children.set(char, new TriedNode())
26+
}
27+
node = node.children.get(char)
28+
}
29+
node.isEnd = true
30+
}
31+
32+
search(word: string): boolean {
33+
let node = this.root
34+
for (const char of word) {
35+
if (!node.children.has(char)) {
36+
return false
37+
}
38+
node = node.children.get(char)
39+
}
40+
return node.isEnd
41+
}
42+
43+
startsWith(prefix: string): boolean {
44+
let node = this.root
45+
for (const char of prefix) {
46+
if (!node.children.has(char)) {
47+
48+
return false
49+
}
50+
node = node.children.get(char)
51+
}
52+
return true
53+
}
54+
}

word-break/hoyeongkwak.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
time complexity : O(n^2k)
3+
space complexity : O(n + m)
4+
*/
5+
6+
function wordBreak(s: string, wordDict: string[]): boolean {
7+
const dp: boolean[] = new Array(s.length + 1).fill(false)
8+
9+
dp[0] = true
10+
11+
const wordSet = new Set(wordDict)
12+
for (let i = 1; i <= s.length; i++) {
13+
for (let j = 0; j < i; j++) {
14+
if (dp[j] && wordSet.has(s.substring(j, i))) {
15+
dp[i] = true
16+
break
17+
}
18+
}
19+
}
20+
return dp[s.length]
21+
};

0 commit comments

Comments
 (0)