Skip to content

Commit 20a2332

Browse files
committed
SOLUTIOINS WEKKS 06
1 parent 2b7256f commit 20a2332

File tree

5 files changed

+147
-0
lines changed

5 files changed

+147
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
function maxArea(height: number[]): number {
2+
let res = 0
3+
let l = 0
4+
let r = height.length - 1
5+
while (l < r) {
6+
const area = (r - l) * Math.min(height[l], height[r])
7+
res = Math.max(res, area)
8+
if (height[l] > height[r]) {
9+
r -= 1
10+
} else {
11+
l += 1
12+
}
13+
}
14+
return res
15+
};
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
O(n)
3+
O(n)
4+
*/
5+
6+
class TriedNode {
7+
children: Map<string, TriedNode>
8+
isEndOfWord: boolean
9+
10+
constructor(){
11+
this.children = new Map()
12+
this.isEndOfWord = false
13+
}
14+
}
15+
16+
class WordDictionary {
17+
root: TriedNode
18+
constructor() {
19+
this.root = new TriedNode()
20+
}
21+
22+
addWord(word: string): void {
23+
let node = this.root
24+
for (const char of word) {
25+
if (!node.children.has(char)) {
26+
node.children.set(char, new TriedNode())
27+
}
28+
node = node.children.get(char)
29+
}
30+
node.isEndOfWord = true
31+
}
32+
33+
search(word: string): boolean {
34+
const searchInNode = (word: string, index: number, node: TriedNode): boolean => {
35+
if (index === word.length) return node.isEndOfWord
36+
37+
const char = word[index]
38+
39+
if (char === '.') {
40+
for (const [, childNode] of node.children) {
41+
if (searchInNode(word, index + 1, childNode)) {
42+
return true
43+
}
44+
}
45+
return false
46+
} else {
47+
if (!node.children.has(char)) {
48+
return false
49+
}
50+
return searchInNode(word, index + 1, node.children.get(char))
51+
}
52+
}
53+
return searchInNode(word, 0, this.root)
54+
}
55+
}
56+
57+
/**
58+
* Your WordDictionary object will be instantiated and called as such:
59+
* var obj = new WordDictionary()
60+
* obj.addWord(word)
61+
* var param_2 = obj.search(word)
62+
*/
63+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function lengthOfLIS(nums: number[]): number {
2+
let res = 1
3+
const dp: number[] = Array.from(nums).fill(1)
4+
for (let i = nums.length - 2; i >= 0; i--) {
5+
let curr = 1
6+
let j = i
7+
while(j < nums.length && curr < res + 1) {
8+
if (nums[j] > nums[i]) {
9+
curr = Math.max(curr, 1 + dp[j])
10+
}
11+
j++
12+
}
13+
dp[i] = curr
14+
res = Math.max(dp[i], res)
15+
}
16+
return res
17+
};

spiral-matrix/hoyeongkwak.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
function spiralOrder(matrix: number[][]): number[] {
2+
const m = matrix.length
3+
const n = matrix[0].length
4+
const size = m * n
5+
let traversed = 0
6+
let top = 0
7+
let bottom = m - 1
8+
let left = 0
9+
let right = n - 1
10+
const output = []
11+
12+
while (traversed < size) {
13+
for (let i = left; traversed < size && i <= right; i++, traversed++) {
14+
output.push(matrix[top][i])
15+
}
16+
top++
17+
18+
for (let i = top; traversed < size && i <= bottom; i++, traversed++) {
19+
output.push(matrix[i][right])
20+
}
21+
right--
22+
23+
for (let i = right; traversed < size && i >= left; i--, traversed++) {
24+
output.push(matrix[bottom][i])
25+
}
26+
bottom--
27+
28+
for (let i = bottom; traversed < size && i >= top; i--, traversed++) {
29+
output.push(matrix[i][left])
30+
}
31+
left++
32+
}
33+
return output
34+
};

valid-parentheses/hoyeongkwak.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function isValid(s: string): boolean {
2+
const stack: string[] = []
3+
const strMap = new Map<string, string>([
4+
['(', ')'],
5+
['[', ']'] ,
6+
['{', '}']
7+
])
8+
for (let str of s) {
9+
if (strMap.has(str)) {
10+
stack.push(strMap.get(str))
11+
} else if (stack.length > 0 && stack[stack.length - 1] === str) {
12+
stack.pop()
13+
} else {
14+
return false
15+
}
16+
}
17+
return stack.length === 0
18+
};

0 commit comments

Comments
 (0)