Skip to content

Commit 94da0df

Browse files
authored
Merge pull request #470 from gitsunmin/main
[gitsunmin] Week 6 Solutions
2 parents 67ab0cd + 94720e1 commit 94da0df

File tree

5 files changed

+153
-0
lines changed

5 files changed

+153
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* https://leetcode.com/problems/container-with-most-water/
3+
* time complexity : O(n)
4+
* space complexity : O(1)
5+
*/
6+
export function maxArea(height: number[]): number {
7+
let s = 0, e = height.length - 1, max = 0;
8+
9+
while (s < e) {
10+
max = Math.max((e - s) * Math.min(height[s], height[e]), max);
11+
if (height[s] < height[e]) s++;
12+
else e--;
13+
}
14+
return max;
15+
};
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* https://leetcode.com/problems/design-add-and-search-words-data-structure
3+
* n: The total number of words stored in the Trie.
4+
* m: The average length of each word.
5+
*
6+
* time complexity:
7+
* - addWord: O(m)
8+
* - search: O(26^m) in the worst case (with multiple wildcards) to O(m) in general cases.
9+
* space complexity:
10+
* - addWord: O(n * m)
11+
* - search: O(n * m)
12+
*/
13+
class TrieNode {
14+
children: Map<string, TrieNode>;
15+
isEnd: boolean;
16+
17+
constructor() {
18+
this.children = new Map();
19+
this.isEnd = false;
20+
}
21+
}
22+
23+
export class WordDictionary {
24+
private root: TrieNode;
25+
26+
constructor() {
27+
this.root = new TrieNode();
28+
}
29+
30+
addWord(word: string): void {
31+
let node = this.root;
32+
33+
for (const char of word) {
34+
if (!node.children.has(char)) node.children.set(char, new TrieNode());
35+
36+
node = node.children.get(char)!;
37+
}
38+
node.isEnd = true;
39+
}
40+
41+
search(word: string): boolean {
42+
return this.searchInNode(word, 0, this.root);
43+
}
44+
45+
private searchInNode(word: string, index: number, node: TrieNode): boolean {
46+
if (index === word.length) return node.isEnd;
47+
48+
const char = word[index];
49+
50+
if (char === '.') {
51+
for (const child of node.children.values()) {
52+
if (this.searchInNode(word, index + 1, child)) return true;
53+
}
54+
return false;
55+
} else {
56+
if (!node.children.has(char)) return false;
57+
return this.searchInNode(word, index + 1, node.children.get(char)!);
58+
}
59+
}
60+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/longest-increasing-subsequence
3+
* time complexity : O(n²)
4+
* space complexity : O(n)
5+
*/
6+
function lengthOfLIS(nums: number[]): number {
7+
const [head] = nums;
8+
const basket = [head];
9+
10+
for (let i = 1; i < nums.length; i++) {
11+
const current = nums[i];
12+
let j = 0;
13+
14+
while (j < basket.length && basket[j] < current) j++;
15+
16+
if (j === basket.length) basket.push(current);
17+
else basket[j] = current;
18+
}
19+
20+
return basket.length;
21+
};

spiral-matrix/gitsunmin.ts

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

valid-parentheses/gitsunmin.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* https://leetcode.com/problems/valid-parentheses/
3+
* time complexity : O(n)
4+
* space complexity : O(n)
5+
*/
6+
7+
type OpeningBracket = '(' | '[' | '{';
8+
type ClosingBracket = ')' | ']' | '}';
9+
10+
const isEmpty = (stack: OpeningBracket[]): boolean => stack.length === 0;
11+
12+
function isValid(s: string): boolean {
13+
const m = new Map<string, ClosingBracket>([
14+
['(', ')'],
15+
['[', ']'],
16+
['{', '}']
17+
]);
18+
const stack: OpeningBracket[] = [];
19+
20+
for (const c of s) {
21+
if (m.has(c)) stack.push(c as OpeningBracket);
22+
else if (isEmpty(stack) || c !== m.get(stack.pop() as OpeningBracket)) return false;
23+
}
24+
return isEmpty(stack);
25+
};

0 commit comments

Comments
 (0)