Skip to content

Commit 1815356

Browse files
authored
Merge pull request #1429 from Jeehay28/main
[Jeehay28] WEEK06 Solutions
2 parents bf0a6f5 + 1cf59f6 commit 1815356

File tree

5 files changed

+260
-0
lines changed

5 files changed

+260
-0
lines changed

container-with-most-water/Jeehay28.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// TC: O(n)
2+
// SC: O(1)
3+
4+
function maxArea(height: number[]): number {
5+
let left = 0;
6+
let right = height.length - 1;
7+
let maxWater = 0;
8+
9+
while (left < right) {
10+
const x = right - left;
11+
const y = Math.min(height[left], height[right]);
12+
13+
maxWater = Math.max(x * y, maxWater);
14+
15+
if (height[left] <= height[right]) {
16+
left += 1;
17+
} else {
18+
right -= 1;
19+
}
20+
}
21+
22+
return maxWater;
23+
}
24+
25+
26+
// ❌ Time Limit Exceeded!
27+
// TC: O(n^2)
28+
29+
// function maxArea(height: number[]): number {
30+
// // input: an integer array -> height
31+
// // output: the maximum amount of water
32+
// // height = [1, 8, 6, 2, 5, 4, 8, 3, 7]
33+
34+
// let maxWater = 0;
35+
36+
// for (let start = 0; start < height.length - 1; start++) {
37+
// for (let end = start + 1; end < height.length; end++) {
38+
// const x = end - start;
39+
// const y = Math.min(height[start], height[end]);
40+
// maxWater = Math.max(maxWater, x * y);
41+
// }
42+
// }
43+
44+
// return maxWater;
45+
// }
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
class WordDictionary {
2+
root: {
3+
[key: string]: any;
4+
};
5+
6+
constructor() {
7+
this.root = { $: true }; // ending
8+
}
9+
10+
// {
11+
// "$": true,
12+
// "b": {
13+
// "$": false,
14+
// "a": {
15+
// "$": false,
16+
// "d": {
17+
// "$": true
18+
// }
19+
// }
20+
// }
21+
// }
22+
23+
// TC: O(w)
24+
// SC: O(w)
25+
addWord(word: string): void {
26+
let node = this.root;
27+
28+
for (const ch of word) {
29+
if (!node[ch]) {
30+
node[ch] = { $: false };
31+
}
32+
node = node[ch];
33+
}
34+
35+
node["$"] = true;
36+
}
37+
38+
// TC: O(26^w)
39+
// SC: O(w)
40+
search(word: string): boolean {
41+
const dfs = (node, idx: number) => {
42+
if (idx === word.length) {
43+
return node["$"];
44+
}
45+
46+
const ch = word[idx];
47+
48+
if (node[ch]) {
49+
return dfs(node[ch], idx + 1);
50+
}
51+
52+
if (ch === ".") {
53+
for (const key of Object.keys(node)) {
54+
if (key !== "$" && dfs(node[key], idx + 1)) {
55+
return true;
56+
}
57+
}
58+
}
59+
60+
return false;
61+
};
62+
63+
return dfs(this.root, 0);
64+
}
65+
}
66+
67+
/**
68+
* Your WordDictionary object will be instantiated and called as such:
69+
* var obj = new WordDictionary()
70+
* obj.addWord(word)
71+
* var param_2 = obj.search(word)
72+
*/
73+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Time Complexity: O(n * log n)
2+
// Space Complexity: O(n)
3+
4+
function lengthOfLIS(nums: number[]): number {
5+
const sub: number[] = []; // O(n)
6+
7+
for (const num of nums) { // O(n)
8+
// lower bound binary search: find the first index where element >= target
9+
let left = 0;
10+
let right = sub.length;
11+
const target = num;
12+
13+
while (left < right) { // O(log n)
14+
let mid = Math.floor((left + right) / 2);
15+
if (sub[mid] < target) {
16+
left = mid + 1;
17+
} else {
18+
right = mid;
19+
}
20+
}
21+
22+
// if target is greater than all elements in sub
23+
if (left === sub.length) {
24+
sub.push(target);
25+
} else {
26+
// replace the first element >= target
27+
sub[left] = target;
28+
}
29+
}
30+
31+
return sub.length;
32+
}
33+

spiral-matrix/Jeehay28.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// TC: O(m * n)
2+
// SC: O(1), if excluding the out array
3+
4+
function spiralOrder(matrix: number[][]): number[] {
5+
let n_rows = matrix.length;
6+
let n_cols = matrix[0].length;
7+
let row = 0;
8+
let col = -1;
9+
let direction = 1;
10+
11+
const output: number[] = [];
12+
13+
while (n_rows > 0 && n_cols > 0) {
14+
// move horizontally: right or left
15+
for (let i = 0; i < n_cols; i++) {
16+
col += direction; // in first iteration, direction = 1
17+
output.push(matrix[row][col]);
18+
}
19+
n_rows -= 1;
20+
21+
// move vertically: down or up
22+
for (let j = 0; j < n_rows; j++) {
23+
row += direction;
24+
output.push(matrix[row][col]);
25+
}
26+
n_cols -= 1;
27+
28+
// change direction
29+
direction *= -1;
30+
}
31+
32+
return output;
33+
}
34+
35+
36+
// TC: O(m * n)
37+
// SC: O(1), if excluding the out array
38+
39+
// function spiralOrder(matrix: number[][]): number[] {
40+
// // move: right -> down -> left -> up -> right -> ...
41+
// // matrix = [[1,2,3],[4,5,6],[7,8,9]]
42+
43+
// let top = 0;
44+
// let bottom = matrix.length - 1;
45+
// let left = 0;
46+
// let right = matrix[0].length - 1;
47+
// let result: number[] = [];
48+
49+
// while (top <= bottom && left <= right) {
50+
// // to the right
51+
// for (let col = left; col <= right; col++) {
52+
// result.push(matrix[top][col]);
53+
// }
54+
// top += 1;
55+
56+
// // down
57+
// for (let row = top; row <= bottom; row++) {
58+
// result.push(matrix[row][right]);
59+
// }
60+
// right -= 1;
61+
62+
// // to the left
63+
// // check needed because top was updated above
64+
// if (top <= bottom) {
65+
// for (let col = right; col >= left; col--) {
66+
// result.push(matrix[bottom][col]);
67+
// }
68+
// bottom -= 1;
69+
// }
70+
71+
// // up
72+
// // check needed because right was updated above
73+
// if (left <= right) {
74+
// for (let row = bottom; row >= top; row--) {
75+
// result.push(matrix[row][left]);
76+
// }
77+
// left += 1;
78+
// }
79+
// }
80+
81+
// return result;
82+
// }
83+

valid-parentheses/Jeehay28.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(n)
3+
4+
function isValid(s: string): boolean {
5+
const map = new Map<string, string>([
6+
["(", ")"],
7+
["{", "}"],
8+
["[", "]"],
9+
]);
10+
11+
let stack: string[] = [];
12+
13+
for (const ch of s) {
14+
if (map.has(ch)) {
15+
stack.push(ch);
16+
} else {
17+
const last = stack.pop();
18+
if (!last || ch !== map.get(last)) {
19+
return false;
20+
}
21+
}
22+
}
23+
24+
return stack.length === 0;
25+
}
26+

0 commit comments

Comments
 (0)