Skip to content

Commit 22b9bb6

Browse files
authored
Merge pull request #1217 from Jeehay28/main
[Jeehay28] WEEK 02 Solutions
2 parents 33a78b3 + 872feec commit 22b9bb6

File tree

5 files changed

+329
-0
lines changed

5 files changed

+329
-0
lines changed

โ€Ž3sum/Jeehay28.ts

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Approach 3
2+
// ๐Ÿ—“๏ธ 2025-04-12
3+
// โณ Time Complexity: O(n log n) + O(n^2) = O(n^2)
4+
// ๐Ÿ’พ Space Complexity: O(1) (excluding output)
5+
6+
function threeSum(nums: number[]): number[][] {
7+
8+
nums.sort((a, b) => a - b); // O(n log n)
9+
let result: number[][] = [];
10+
11+
for (let i = 0; i < nums.length; i++) {
12+
13+
if (i > 0 && nums[i - 1] == nums[i]) continue; // skip duplicate i
14+
15+
let low = i + 1, high = nums.length - 1;
16+
// two-pointer scan (low and high) -> takes up to O(n) time per iteration
17+
while (low < high) {
18+
const threeSum = nums[i] + nums[low] + nums[high];
19+
if (threeSum < 0) {
20+
low += 1;
21+
} else if (threeSum > 0) {
22+
high -= 1;
23+
} else {
24+
result.push([nums[i], nums[low], nums[high]]);
25+
26+
while (low < high && nums[low] === nums[low + 1]) low += 1 // skip duplicate low
27+
while (low < high && nums[high] === nums[high - 1]) high -= 1 // skip duplicate high
28+
29+
low += 1;
30+
high -= 1;
31+
32+
}
33+
}
34+
}
35+
return result
36+
}
37+
38+
39+
// Approach 2
40+
// ๐Ÿ—“๏ธ 2025-04-11
41+
// โŒ Time Limit Exceeded 313 / 314 testcases passed
42+
// โณ Time Complexity: O(n^2)
43+
// ๐Ÿ’พ Space Complexity: O(n^2)
44+
45+
// function threeSum(nums: number[]): number[][] {
46+
// const result: number[][] = [];
47+
// const triplets = new Set<string>();
48+
49+
// for (let i = 0; i < nums.length - 2; i++) {
50+
// const seen = new Set<number>();
51+
// for (let j = i + 1; j < nums.length; j++) {
52+
// const twoSum = nums[i] + nums[j];
53+
// if (seen.has(-twoSum)) {
54+
// const triplet = [nums[i], nums[j], -twoSum].sort((a, b) => a - b); // O(log 3) = O(1)
55+
// const key = triplet.join(",");
56+
// if (!triplets.has(key)) {
57+
// triplets.add(key);
58+
// result.push(triplet);
59+
// }
60+
// }
61+
// seen.add(nums[j]);
62+
// }
63+
// }
64+
// return result;
65+
// }
66+
67+
68+
// Approach 1
69+
// ๐Ÿ—“๏ธ 2025-04-11
70+
// โŒ Time Limit Exceeded!
71+
// โณ Time Complexity: O(n^3)
72+
// ๐Ÿ’พ Space Complexity: O(n^2)
73+
74+
// function threeSum(nums: number[]): number[][] {
75+
76+
// let triplets = new Set<string>();
77+
// let result: number[][] = [];
78+
79+
// // const set = new Set();
80+
// // set.add([1, 2, 3]);
81+
// // set.add([1, 2, 3]);
82+
// // console.log(set); // contains BOTH arrays
83+
// // Set(2) { [ 1, 2, 3 ], [ 1, 2, 3 ] }
84+
85+
// for (let i = 0; i < nums.length - 2; i++) {
86+
// for (let j = i + 1; j < nums.length - 1; j++) {
87+
// for (let k = j + 1; k < nums.length; k++) {
88+
// if (nums[i] + nums[j] + nums[k] === 0) {
89+
// const triplet = [nums[i], nums[j], nums[k]].sort((a, b) => a - b);
90+
// const key = triplet.join(",");
91+
// if (!triplets.has(key)) {
92+
// triplets.add(key);
93+
// result.push(triplet)
94+
// }
95+
// }
96+
// }
97+
// }
98+
// }
99+
100+
// return result;
101+
// };

โ€Žclimbing-stairs/Jeehay28.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Approach 2: DFS with Memoization
2+
// ๐Ÿ—“๏ธ 2025-04-06
3+
// โณ Time Complexity: O(n)
4+
// ๐Ÿ’พ Space Complexity: O(n)
5+
6+
function climbStairs(n: number): number {
7+
const memo = new Map<number, number>();
8+
9+
const dfs = (step: number) => {
10+
if (step > n) return 0;
11+
if (step === n) return 1;
12+
if (memo.has(step)) return memo.get(step);
13+
14+
const result = dfs(step + 1) + dfs(step + 2);
15+
memo.set(step, result);
16+
return result;
17+
};
18+
19+
return dfs(0);
20+
}
21+
22+
23+
// Approach 1: DFS (depth-first search)
24+
// โŒ Time Limit Exceeded (TLE) error!
25+
// โณ Time Complexity: O(2^n)
26+
// ๐Ÿ’พ Space Complexity: O(n)
27+
// The maximum depth of the recursive call stack is n, because we go from step = 0 to step = n.
28+
29+
// function climbStairs(n: number): number {
30+
// // 1 -> 1 -> 1
31+
// // 1 -> 2
32+
// // 2 -> 1
33+
34+
// const dfs = (step: number) => {
35+
// // reach the top
36+
// if (step > n) return 0;
37+
// if (step === n) return 1;
38+
39+
// return dfs(step + 1) + dfs(step + 2);
40+
// };
41+
42+
// return dfs(0);
43+
// }
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Approach 2
2+
// ๐Ÿ—“๏ธ 2025-04-08
3+
// โณ Time Complexity: O(n + n) = O(n)
4+
// ๐Ÿ’พ Space Complexity: O(1)
5+
// result: required output โ†’ not counted
6+
// before, after: two scalar variables โ†’ O(1)
7+
// So, the total extra space is O(1)
8+
9+
function productExceptSelf(nums: number[]): number[] {
10+
const result: number[] = new Array(nums.length).fill(1);
11+
let before = 1;
12+
let after = 1;
13+
14+
for (let i = 0; i < nums.length - 1; i++) {
15+
before *= nums[i];
16+
result[i + 1] *= before;
17+
}
18+
19+
for (let i = nums.length - 1; i > 0; i--) {
20+
after *= nums[i];
21+
result[i - 1] *= after;
22+
}
23+
return result;
24+
}
25+
26+
27+
// Approach 1
28+
// ๐Ÿ—“๏ธ 2025-04-08
29+
// โณ Time Complexity: O(n + n + n) = O(n)
30+
// ๐Ÿ’พ Space Complexity: O(n + n) = O(n)
31+
32+
// When we analyze space complexity,
33+
// we only count extra space used in addition to the input and required output.
34+
// โœ… before โ†’ O(n) extra
35+
// โœ… after โ†’ O(n) extra
36+
// ๐Ÿšซ result is the output, so itโ€™s not counted toward space complexity
37+
38+
// function productExceptSelf(nums: number[]): number[] {
39+
// // nums: an array of size n
40+
// // before: nums[0], nums[1].., nums[i-2], nums[i-1]
41+
// // after: nums[i+1], nums[i+2],..., nums[n-1]
42+
43+
// // input: [1, 2, 3, 4]
44+
// // before: [1, 1, 2, 6]
45+
// // after: [24, 12, 4, 1]
46+
// // product: [24, 12, 8, 6]
47+
48+
// const before = new Array(nums.length).fill(1);
49+
// const after = new Array(nums.length).fill(1);
50+
// const result: number[] = [];
51+
52+
// for (let i = 0; i < nums.length - 1; i++) {
53+
// before[i + 1] = before[i] * nums[i];
54+
// }
55+
56+
// for (let i = nums.length - 1; i > 0; i--) {
57+
// after[i - 1] = after[i] * nums[i];
58+
// }
59+
60+
// for (let i = 0; i < nums.length; i++) {
61+
// result[i] = before[i] * after[i];
62+
// }
63+
64+
// return result;
65+
// }

โ€Žvalid-anagram/Jeehay28.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Approach 2
2+
// ๐Ÿ—“๏ธ 2025-04-06
3+
// โณ Time Complexity: O(n)
4+
// ๐Ÿ’พ Space Complexity: O(n)
5+
6+
function isAnagram(s: string, t: string): boolean {
7+
const map = new Map<string, number>();
8+
9+
for (const char of s) {
10+
map.set(char, (map.get(char) || 0) + 1);
11+
}
12+
13+
for (const char of t) {
14+
if (!map.has(char)) return false;
15+
map.set(char, map.get(char)! - 1);
16+
if (map.get(char) === 0) {
17+
map.delete(char);
18+
}
19+
}
20+
21+
return map.size === 0;
22+
}
23+
24+
25+
// Approach 1
26+
// โณ Time Complexity: O(n)
27+
// ๐Ÿ’พ Space Complexity: O(n)
28+
29+
// function isAnagram(s: string, t: string): boolean {
30+
// const s_map = new Map<string, number>();
31+
// const t_map = new Map<string, number>();
32+
33+
// for (const char of s) {
34+
// s_map.set(char, (s_map.get(char) || 0) + 1);
35+
// }
36+
37+
// for (const char of t) {
38+
// t_map.set(char, (t_map.get(char) || 0) + 1);
39+
// }
40+
41+
// // Compare the keys and values of both maps
42+
// if ([...s_map.keys()].length !== [...t_map.keys()].length) {
43+
// return false;
44+
// } else {
45+
// for (const char of [...s_map.keys()]) {
46+
// if (s_map.get(char) !== t_map.get(char)) {
47+
// // Lookup operations in Map are O(1)
48+
// return false;
49+
// }
50+
// }
51+
// }
52+
53+
// return true;
54+
// }
55+
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Approach 2
2+
// ๐Ÿ—“๏ธ 2025-04-11
3+
// โณ Time Complexity: O(n)
4+
// ๐Ÿ’พ Space Complexity: O(h) โ†’ O(log n) for balanced, O(n) for skewed trees
5+
6+
class TreeNode {
7+
val: number;
8+
left: TreeNode | null;
9+
right: TreeNode | null;
10+
11+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
12+
this.val = val === undefined ? 0 : val;
13+
this.left = left === undefined ? null : left;
14+
this.right = right === undefined ? null : right;
15+
}
16+
}
17+
18+
function isValidBST(root: TreeNode | null): boolean {
19+
// ๐ŸŸก ์ค‘์œ„ ์ˆœํšŒ (Inorder Traversal)
20+
// ๋ฐฉ๋ฌธ ์ˆœ์„œ: ์™ผ์ชฝ ์ž์‹ โ†’ ํ˜„์žฌ ๋…ธ๋“œ โ†’ ์˜ค๋ฅธ์ชฝ ์ž์‹
21+
22+
let maxVal = -Infinity;
23+
24+
const dfs = (node: TreeNode | null) => {
25+
if (!node) return true;
26+
27+
if (!dfs(node.left)) return false;
28+
29+
if (node.val <= maxVal) return false;
30+
31+
maxVal = node.val;
32+
33+
if (!dfs(node.right)) return false;
34+
35+
return true;
36+
};
37+
38+
return dfs(root);
39+
}
40+
41+
// Approach 1
42+
// ๐Ÿ—“๏ธ 2025-04-11
43+
// โณ Time Complexity: O(n)
44+
// ๐Ÿ’พ Space Complexity: O(h) โ†’ O(log n) for balanced, O(n) for skewed trees
45+
46+
// function isValidBST(root: TreeNode | null): boolean {
47+
// // ์ขŒ์ธก ์„œ๋ธŒ ํŠธ๋ฆฌ๋กœ ๋‚ด๋ ค๊ฐˆ ๋•Œ:
48+
// // ํ•˜ํ•œ๊ฐ’: ๋ถ€๋ชจ ๋…ธ๋“œ ํ•˜ํ•œ๊ฐ’
49+
// // ์ƒํ•œ๊ฐ’: ๋ถ€๋ชจ ๋…ธ๋“œ ๊ฐ’
50+
// // ์šฐ์ธก ์„œ๋ธŒ ํŠธ๋ฆฌ๋กœ ๋‚ด๋ ค๊ฐˆ ๋•Œ:
51+
// // ์šฐ์ธก ํ•˜ํ•œ๊ฐ’: ๋ถ€๋ชจ ๋…ธ๋“œ ๊ฐ’
52+
// // ์šฐ์ธก ์ƒํ•œ๊ฐ’: ๋ถ€๋ชจ ๋…ธ๋“œ์˜ ์ƒํ•œ ๊ฐ’
53+
// // ๐ŸŸข ์ „์œ„ ์ˆœํšŒ (Preorder Traversal)
54+
// // ๋ฐฉ๋ฌธ ์ˆœ์„œ: ํ˜„์žฌ ๋…ธ๋“œ โ†’ ์™ผ์ชฝ ์ž์‹ โ†’ ์˜ค๋ฅธ์ชฝ ์ž์‹
55+
56+
// const dfs = (node: TreeNode | null, low: number, high: number) => {
57+
// if (!node) return true;
58+
59+
// if (!(node.val > low && node.val < high)) return false;
60+
61+
// return dfs(node.left, low, node.val) && dfs(node.right, node.val, high);
62+
// };
63+
64+
// return dfs(root, -Infinity, Infinity);
65+
// }

0 commit comments

Comments
ย (0)