Skip to content
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
24 changes: 24 additions & 0 deletions container-with-most-water/juhui-jeong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
시간복잡도: O(n)
공간복잡도: O(1)
포인터까지는 생각했으나, 그이후로는 해결법이 떠오르지 않아, 알고달레 풀이를 보고 해결.
*/
function maxArea(height: number[]): number {
let maxArea = 0;
let left = 0;
let right = height.length - 1;

while (left < right) {
let area = (right - left) * Math.min(height[left], height[right]);

if (maxArea < area) maxArea = area;

if (height[left] > height[right]) {
right -= 1;
} else {
left += 1;
}
}
return maxArea;
}
61 changes: 61 additions & 0 deletions design-add-and-search-words-data-structure/juhui-jeong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
시간복잡도: O(L)
공간복잡도: O(L)
Trie는 검색을 빠르게 해주지만,
. 와일드카드는 DFS 분기를 유발해 최악의 경우 시간 복잡도가 커질 수 있다.
DFS가 필요한 검색 문제에 대한 공부 필요.
*/
class TrieNode {
children: Map<string, TrieNode>;
isEnd: boolean;

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

class WordDictionary {
private root: TrieNode;

constructor() {
this.root = new TrieNode();
}

addWord(word: string): void {
let node = this.root;

for (const ch of word) {
if (!node.children.has(ch)) {
node.children.set(ch, new TrieNode());
}
node = node.children.get(ch)!;
}
node.isEnd = true;
}

search(word: string): boolean {
const dfs = (index: number, node: TrieNode): boolean => {
if (index === word.length) {
return node.isEnd;
}

const ch = word[index];

if (ch === '.') {
for (const child of node.children.values()) {
if (dfs(index + 1, child)) return true;
}
return false;
}

const next = node.children.get(ch);
if (!next) return false;

return dfs(index + 1, next);
};

return dfs(0, this.root);
}
}
35 changes: 35 additions & 0 deletions valid-parentheses/juhui-jeong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
시간복잡도: O(n)
공간복잡도: O(n)
*/
function isValid(s: string): boolean {
if (s.length % 2 !== 0) {
return false;
}

const stack: string[] = [];
const pair: Map<string, string> = new Map<string, string>([
[')', '('],
['}', '{'],
[']', '['],
]);

for (const char of s) {
// close brackts인지 확인
if (pair.has(char)) {
let topItem = stack.pop();
// pair 확인
if (topItem !== pair.get(char)) {
return false;
}
} else {
stack.push(char);
}
}

if (stack.length === 0) {
return true;
} else {
return false;
}
}