Skip to content

Commit 69a3855

Browse files
authored
Merge pull request #1765 from hyer0705/main
[hyer0705] WEEK 02 solutions
2 parents 3da51cb + 439040f commit 69a3855

File tree

5 files changed

+110
-0
lines changed

5 files changed

+110
-0
lines changed

3sum/hyer0705.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
function threeSum(nums: number[]): number[][] {
2+
nums.sort((a, b) => a - b);
3+
4+
const result: number[][] = [];
5+
for (let i = 0; i < nums.length - 2; i++) {
6+
if (i > 0 && nums[i] === nums[i - 1]) continue;
7+
const target = -nums[i];
8+
9+
let j = i + 1;
10+
let k = nums.length - 1;
11+
while (j < k) {
12+
const currentSum = nums[j] + nums[k];
13+
14+
if (target < currentSum) {
15+
k--;
16+
} else if (target > currentSum) {
17+
j++;
18+
} else {
19+
result.push([nums[i], nums[j], nums[k]]);
20+
21+
j++;
22+
k--;
23+
24+
while (j < k && nums[j] === nums[j - 1]) j++;
25+
while (j < k && nums[k] === nums[k + 1]) k--;
26+
}
27+
}
28+
}
29+
30+
return result;
31+
}

climbing-stairs/hyer0705.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function climbStairs(n: number): number {
2+
const dp: number[] = Array.from({ length: n + 1 }, () => 0);
3+
dp[0] = 1;
4+
dp[1] = 1;
5+
6+
for (let i = 2; i < n + 1; i++) {
7+
dp[i] = dp[i - 1] + dp[i - 2];
8+
}
9+
10+
return dp[n];
11+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function productExceptSelf(nums: number[]): number[] {
2+
const n = nums.length;
3+
const result = Array(n).fill(1);
4+
5+
for (let i = 1; i < n; i++) {
6+
result[i] = nums[i - 1] * result[i - 1];
7+
}
8+
9+
let suffixProduct = 1;
10+
for (let i = n - 1; i >= 0; i--) {
11+
result[i] = result[i] * suffixProduct;
12+
suffixProduct *= nums[i];
13+
}
14+
15+
return result;
16+
}

valid-anagram/hyer0705.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
if (s.length !== t.length) return false;
3+
4+
const sCountMap = new Map<string, number>();
5+
const tCountMap = new Map<string, number>();
6+
7+
for (let i = 0; i < s.length; i++) {
8+
const currentS = s[i];
9+
const currentT = t[i];
10+
11+
sCountMap.set(currentS, (sCountMap.get(currentS) || 0) + 1);
12+
tCountMap.set(currentT, (tCountMap.get(currentT) || 0) + 1);
13+
}
14+
15+
for (const [k, v] of sCountMap) {
16+
if (!tCountMap.has(k) || tCountMap.get(k) !== v) return false;
17+
}
18+
19+
return true;
20+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* class TreeNode {
4+
* val: number
5+
* left: TreeNode | null
6+
* right: TreeNode | null
7+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
8+
* this.val = (val===undefined ? 0 : val)
9+
* this.left = (left===undefined ? null : left)
10+
* this.right = (right===undefined ? null : right)
11+
* }
12+
* }
13+
*/
14+
15+
function isValidBST(root: TreeNode | null): boolean {
16+
const queue: [TreeNode, number, number][] = [[root, -Infinity, +Infinity]];
17+
18+
while (queue.length > 0) {
19+
const [currentNode, minVal, maxVal] = queue.shift();
20+
21+
if (minVal >= currentNode.val || currentNode.val >= maxVal) return false;
22+
23+
if (currentNode.left) {
24+
queue.push([currentNode.left, minVal, currentNode.val]);
25+
}
26+
if (currentNode.right) {
27+
queue.push([currentNode.right, currentNode.val, maxVal]);
28+
}
29+
}
30+
31+
return true;
32+
}

0 commit comments

Comments
 (0)