Skip to content

Commit a155a5c

Browse files
committed
Week 06 solution commit
1 parent 69fcd06 commit a155a5c

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
시간복잡도: O(n)
3+
공간복잡도: O(1)
4+
포인터까지는 생각했으나, 그이후로는 해결법이 떠오르지 않아, 알고달레 풀이를 보고 해결.
5+
6+
*/
7+
function maxArea(height: number[]): number {
8+
let maxArea = 0;
9+
let left = 0;
10+
let right = height.length - 1;
11+
12+
while (left < right) {
13+
let area = (right - left) * Math.min(height[left], height[right]);
14+
15+
if (maxArea < area) maxArea = area;
16+
17+
if (height[left] > height[right]) {
18+
right -= 1;
19+
} else {
20+
left += 1;
21+
}
22+
}
23+
return maxArea;
24+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
시간복잡도: O(L)
3+
공간복잡도: O(L)
4+
Trie는 검색을 빠르게 해주지만,
5+
. 와일드카드는 DFS 분기를 유발해 최악의 경우 시간 복잡도가 커질 수 있다.
6+
7+
DFS가 필요한 검색 문제에 대한 공부 필요.
8+
*/
9+
class TrieNode {
10+
children: Map<string, TrieNode>;
11+
isEnd: boolean;
12+
13+
constructor() {
14+
this.children = new Map();
15+
this.isEnd = false;
16+
}
17+
}
18+
19+
class WordDictionary {
20+
private root: TrieNode;
21+
22+
constructor() {
23+
this.root = new TrieNode();
24+
}
25+
26+
addWord(word: string): void {
27+
let node = this.root;
28+
29+
for (const ch of word) {
30+
if (!node.children.has(ch)) {
31+
node.children.set(ch, new TrieNode());
32+
}
33+
node = node.children.get(ch)!;
34+
}
35+
node.isEnd = true;
36+
}
37+
38+
search(word: string): boolean {
39+
const dfs = (index: number, node: TrieNode): boolean => {
40+
if (index === word.length) {
41+
return node.isEnd;
42+
}
43+
44+
const ch = word[index];
45+
46+
if (ch === '.') {
47+
for (const child of node.children.values()) {
48+
if (dfs(index + 1, child)) return true;
49+
}
50+
return false;
51+
}
52+
53+
const next = node.children.get(ch);
54+
if (!next) return false;
55+
56+
return dfs(index + 1, next);
57+
};
58+
59+
return dfs(0, this.root);
60+
}
61+
}

valid-parentheses/juhui-jeong.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
시간복잡도: O(n)
3+
공간복잡도: O(n)
4+
*/
5+
function isValid(s: string): boolean {
6+
if (s.length % 2 !== 0) {
7+
return false;
8+
}
9+
10+
const stack: string[] = [];
11+
const pair: Map<string, string> = new Map<string, string>([
12+
[')', '('],
13+
['}', '{'],
14+
[']', '['],
15+
]);
16+
17+
for (const char of s) {
18+
// close brackts인지 확인
19+
if (pair.has(char)) {
20+
let topItem = stack.pop();
21+
// pair 확인
22+
if (topItem !== pair.get(char)) {
23+
return false;
24+
}
25+
} else {
26+
stack.push(char);
27+
}
28+
}
29+
30+
if (stack.length === 0) {
31+
return true;
32+
} else {
33+
return false;
34+
}
35+
}

0 commit comments

Comments
 (0)