Skip to content

[hoyeongkwak] SOLUTIOINS WEEKS 06 #1457

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
5 commits merged into from
May 17, 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
15 changes: 15 additions & 0 deletions container-with-most-water/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function maxArea(height: number[]): number {
let res = 0
let l = 0
let r = height.length - 1
while (l < r) {
const area = (r - l) * Math.min(height[l], height[r])
res = Math.max(res, area)
if (height[l] > height[r]) {
r -= 1
} else {
l += 1
}
}
return res
};
63 changes: 63 additions & 0 deletions design-add-and-search-words-data-structure/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
O(n)
O(n)
*/

class TriedNode {
children: Map<string, TriedNode>
isEndOfWord: boolean

constructor(){
this.children = new Map()
this.isEndOfWord = false
}
}

class WordDictionary {
root: TriedNode
constructor() {
this.root = new TriedNode()
}

addWord(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.isEndOfWord = true
}

search(word: string): boolean {
const searchInNode = (word: string, index: number, node: TriedNode): boolean => {
if (index === word.length) return node.isEndOfWord

const char = word[index]

if (char === '.') {
for (const [, childNode] of node.children) {
if (searchInNode(word, index + 1, childNode)) {
return true
}
}
return false
} else {
if (!node.children.has(char)) {
return false
}
return searchInNode(word, index + 1, node.children.get(char))
}
}
return searchInNode(word, 0, this.root)
}
}

/**
* Your WordDictionary object will be instantiated and called as such:
* var obj = new WordDictionary()
* obj.addWord(word)
* var param_2 = obj.search(word)
*/

17 changes: 17 additions & 0 deletions longest-increasing-subsequence/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function lengthOfLIS(nums: number[]): number {
let res = 1
const dp: number[] = Array.from(nums).fill(1)
for (let i = nums.length - 2; i >= 0; i--) {
let curr = 1
let j = i
while(j < nums.length && curr < res + 1) {
if (nums[j] > nums[i]) {
curr = Math.max(curr, 1 + dp[j])
}
j++
}
dp[i] = curr
res = Math.max(dp[i], res)
}
return res
};
34 changes: 34 additions & 0 deletions spiral-matrix/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
function spiralOrder(matrix: number[][]): number[] {
const m = matrix.length
const n = matrix[0].length
const size = m * n
let traversed = 0
let top = 0
let bottom = m - 1
let left = 0
let right = n - 1
const output = []

while (traversed < size) {
for (let i = left; traversed < size && i <= right; i++, traversed++) {
output.push(matrix[top][i])
}
top++

for (let i = top; traversed < size && i <= bottom; i++, traversed++) {
output.push(matrix[i][right])
}
right--

for (let i = right; traversed < size && i >= left; i--, traversed++) {
output.push(matrix[bottom][i])
}
bottom--

for (let i = bottom; traversed < size && i >= top; i--, traversed++) {
output.push(matrix[i][left])
}
left++
}
return output
};
18 changes: 18 additions & 0 deletions valid-parentheses/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
function isValid(s: string): boolean {
const stack: string[] = []
const strMap = new Map<string, string>([
['(', ')'],
['[', ']'] ,
['{', '}']
])
for (let str of s) {
if (strMap.has(str)) {
stack.push(strMap.get(str))
} else if (stack.length > 0 && stack[stack.length - 1] === str) {
stack.pop()
} else {
return false
}
}
return stack.length === 0
};