Skip to content

[hoyeongkwak] Week 05 Solutions #1396

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions best-time-to-buy-and-sell-stock/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
time complexity : O(n)
space complexity : O(1)
*/
function maxProfit(prices: number[]): number {
let left = 0
let right = 0
let maxProfit = 0

while (right < prices.length) {
const curProfit = prices[right] - prices[left]
if (prices[left] < prices[right]) {
maxProfit = Math.max(curProfit, maxProfit)
} else {
left = right
}
right += 1
}
return maxProfit
};
18 changes: 18 additions & 0 deletions group-anagrams/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
time complexity : O(n * mlogm)
space complexity : O(n * m)
*/
function groupAnagrams(strs: string[]): string[][] {
const strMap = new Map<string, string[]>()
for (let i = 0; i < strs.length; i++) {
const sortedStr = strs[i].split('').sort().join('')
if (!strMap.has(sortedStr)) {
strMap.set(sortedStr, [strs[i]])
} else {
const prevArr = strMap.get(sortedStr)
prevArr.push(strs[i])
strMap.set(sortedStr, prevArr)
}
}
return Array.from(strMap.values())
};
54 changes: 54 additions & 0 deletions implement-trie-prefix-tree/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
time complexity : O(n)
space complexity : O(n)
*/
class TriedNode {
children: Map<string, TriedNode>
isEnd: boolean
constructor(){
this.children = new Map()
this.isEnd = false
}
}

class Trie {
root: TriedNode

constructor() {
this.root = new TriedNode()
}

insert(word: string): void {
let node = this.root
for (const char of word) {
if (!node.children.has(char)) {
node.children.set(char, new TriedNode())
}
node = node.children.get(char)
}
node.isEnd = true
}

search(word: string): boolean {
let node = this.root
for (const char of word) {
if (!node.children.has(char)) {
return false
}
node = node.children.get(char)
}
return node.isEnd
}

startsWith(prefix: string): boolean {
let node = this.root
for (const char of prefix) {
if (!node.children.has(char)) {

return false
}
node = node.children.get(char)
}
return true
}
}
21 changes: 21 additions & 0 deletions word-break/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
time complexity : O(n^2k)
space complexity : O(n + m)
*/

function wordBreak(s: string, wordDict: string[]): boolean {
const dp: boolean[] = new Array(s.length + 1).fill(false)

dp[0] = true

const wordSet = new Set(wordDict)
for (let i = 1; i <= s.length; i++) {
for (let j = 0; j < i; j++) {
if (dp[j] && wordSet.has(s.substring(j, i))) {
dp[i] = true
break
}
}
}
return dp[s.length]
};