Skip to content

Commit bf31e21

Browse files
authored
Merge pull request #1729 from sooooo-an/main
[sooooo-an] WEEK 02 solutions
2 parents 2063129 + 32633cc commit bf31e21

File tree

5 files changed

+103
-0
lines changed

5 files changed

+103
-0
lines changed

3sum/sooooo-an.ts

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

climbing-stairs/sooooo-an.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function climbStairs(n: number): number {
2+
type Cache = { [key: number]: number };
3+
4+
const cache: Cache = {
5+
2: 2,
6+
1: 1,
7+
};
8+
9+
const fibonacci = (n: number, cache: Cache) => {
10+
if (n in cache) {
11+
return cache[n];
12+
} else {
13+
cache[n] = fibonacci(n - 2, cache) + fibonacci(n - 1, cache);
14+
return cache[n];
15+
}
16+
};
17+
18+
return fibonacci(n, cache);
19+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function productExceptSelf(nums: number[]): number[] {
2+
const results = [];
3+
4+
let left = 1;
5+
let right = 1;
6+
7+
for (let i = 0; i < nums.length; i++) {
8+
results[i] = left;
9+
left *= nums[i];
10+
}
11+
12+
for (let i = nums.length - 1; i >= 0; i--) {
13+
results[i] *= right;
14+
right *= nums[i];
15+
}
16+
17+
return results;
18+
}

valid-anagram/sooooo-an.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
if (s.length !== t.length) return false;
3+
4+
const objS: { [key: string]: number } = {};
5+
6+
for (const str of s) {
7+
objS[str] = (objS[str] ?? 0) + 1;
8+
}
9+
10+
for (const str of t) {
11+
if (!objS[str]) {
12+
return false;
13+
}
14+
objS[str]--;
15+
}
16+
17+
return true;
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
function isValidBST(root: TreeNode | null): boolean {
2+
return validateBSTHelper(root, -Infinity, Infinity);
3+
}
4+
5+
const validateBSTHelper = (
6+
root: TreeNode | null,
7+
minValue: number,
8+
maxValue: number
9+
): boolean => {
10+
if (root === null) {
11+
return true;
12+
}
13+
if (root.val <= minValue || root.val >= maxValue) {
14+
return false;
15+
}
16+
const leftIsValid = validateBSTHelper(root.left, minValue, root.val);
17+
return leftIsValid && validateBSTHelper(root.right, root.val, maxValue);
18+
};

0 commit comments

Comments
 (0)