Skip to content

Commit 189509b

Browse files
authored
Merge pull request #468 from HC-kang/main
[강희찬] WEEK 6 Solution
2 parents 17504f2 + a33714f commit 189509b

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

container-with-most-water/HC-kang.ts

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* https://leetcode.com/problems/container-with-most-water/
3+
* T.C. O(n)
4+
* S.C. O(1)
5+
*/
6+
function maxArea(height: number[]): number {
7+
let [res, i, j] = [0, 0, height.length - 1];
8+
9+
while (i < j) {
10+
const volume = Math.min(height[i], height[j]) * (j - i);
11+
res = Math.max(res, volume);
12+
13+
if (height[i] < height[j]) {
14+
i++;
15+
} else {
16+
j--;
17+
}
18+
}
19+
20+
return res;
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* https://leetcode.com/problems/design-add-and-search-words-data-structure
3+
*/
4+
// Using Trie
5+
class WordDictionary {
6+
constructor(private root: Record<string, any> = {}) {}
7+
8+
/**
9+
* T.C. O(L) L: length of a word
10+
* S.C. O(L)
11+
*/
12+
addWord(word: string): void {
13+
let node = this.root;
14+
for (const char of word) {
15+
if (!node[char]) {
16+
node[char] = {};
17+
}
18+
node = node[char];
19+
}
20+
node['isEnd'] = true;
21+
}
22+
23+
/**
24+
* T.C. O(N) - there are only 2 dots in the word(26 * 26 * N)
25+
* S.C. O(N * L) N: number of words, L: length of a word
26+
*/
27+
search(word: string): boolean {
28+
return this.dfs(word, this.root);
29+
}
30+
31+
private dfs(word: string, node: Record<string, any>): boolean {
32+
for (let i = 0; i < word.length; i++) {
33+
if (word[i] === '.') {
34+
for (const key in node) {
35+
if (this.dfs(word.slice(i + 1), node[key])) {
36+
return true;
37+
}
38+
}
39+
return false;
40+
}
41+
if (!node[word[i]]) {
42+
return false;
43+
}
44+
node = node[word[i]];
45+
}
46+
return !!node['isEnd'];
47+
}
48+
}
49+
50+
// Using Array and Set
51+
class WordDictionary {
52+
constructor(
53+
private words: Set<string>[] = Array.from({ length: 25 }, () => new Set())
54+
) {}
55+
56+
/**
57+
* T.C. O(1)
58+
* S.C. O(N * L)
59+
*/
60+
addWord(word: string): void {
61+
this.words[word.length - 1].add(word);
62+
}
63+
64+
/**
65+
* T.C. O(N * L) N: number of words, L: length of a word
66+
* S.C. O(1)
67+
*/
68+
search(word: string): boolean {
69+
const hasDot = word.indexOf('.') !== -1;
70+
const set = this.words[word.length - 1];
71+
72+
if (!hasDot) {
73+
return set.has(word);
74+
}
75+
76+
for (const w of set) {
77+
let i = 0;
78+
while (i < word.length) {
79+
if (word[i] == '.') {
80+
i++;
81+
continue;
82+
}
83+
if (word[i] !== w[i]) {
84+
break;
85+
}
86+
i++;
87+
}
88+
89+
if (i === word.length) {
90+
return true;
91+
}
92+
}
93+
return false;
94+
}
95+
}
96+
97+
/**
98+
* Your WordDictionary object will be instantiated and called as such:
99+
* var obj = new WordDictionary()
100+
* obj.addWord(word)
101+
* var param_2 = obj.search(word)
102+
*/
+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* https://leetcode.com/problems/longest-increasing-subsequence
3+
* T.C. O(nlogn)
4+
* S.C. O(n)
5+
*/
6+
function lengthOfLIS(nums: number[]): number {
7+
const sub: number[] = [];
8+
9+
function findSlot(num: number): number {
10+
let left = 0;
11+
let right = sub.length - 1;
12+
13+
while (left <= right) {
14+
const mid = Math.floor((left + right) / 2);
15+
if (sub[mid] < num) {
16+
left = mid + 1;
17+
} else {
18+
right = mid - 1;
19+
}
20+
}
21+
22+
return left;
23+
}
24+
25+
for (let i = 0; i < nums.length; i++) {
26+
const num = nums[i];
27+
const slot = findSlot(num);
28+
sub[slot] = num;
29+
}
30+
31+
return sub.length;
32+
}

spiral-matrix/HC-kang.ts

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/**
2+
* https://leetcode.com/problems/spiral-matrix
3+
* T.C. O(m * n)
4+
* S.C. O(m * n)
5+
*/
6+
function spiralOrder(matrix: number[][]): number[] {
7+
const clockwise = [
8+
[0, 1],
9+
[1, 0],
10+
[0, -1],
11+
[-1, 0],
12+
];
13+
let currentDirection = 0;
14+
15+
const visited = new Array(matrix.length)
16+
.fill(0)
17+
.map(() => new Array(matrix[0].length).fill(false));
18+
19+
const result: number[] = [];
20+
21+
let row = 0;
22+
let col = 0;
23+
24+
while (result.length < matrix.length * matrix[0].length) {
25+
result.push(matrix[row][col]);
26+
visited[row][col] = true;
27+
28+
const nextRow = row + clockwise[currentDirection][0];
29+
const nextCol = col + clockwise[currentDirection][1];
30+
31+
if (
32+
nextRow < 0 || nextRow >= matrix.length ||
33+
nextCol < 0 || nextCol >= matrix[0].length ||
34+
visited[nextRow][nextCol]
35+
) {
36+
currentDirection = (currentDirection + 1) % 4;
37+
}
38+
39+
row += clockwise[currentDirection][0];
40+
col += clockwise[currentDirection][1];
41+
}
42+
43+
return result;
44+
}

valid-parentheses/HC-kang.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* https://leetcode.com/problems/valid-parentheses
3+
* T.C. O(n)
4+
* S.C. O(n)
5+
*/
6+
function isValid(s: string): boolean {
7+
const pairs: Record<string, string> = { '{': '}', '[': ']', '(': ')' };
8+
const stack: string[] = [];
9+
10+
if (s.length % 2 == 1) return false;
11+
12+
for (const char of s) {
13+
if (pairs[char]) stack.push(char);
14+
else if (char != pairs[stack.pop()!]) return false;
15+
}
16+
return !stack.length;
17+
}

0 commit comments

Comments
 (0)