Skip to content

Commit 4e4f68d

Browse files
authored
Merge pull request #2065 from casentino/main
[casentino] WEEK 02 solutions
2 parents 18419c8 + 08497f1 commit 4e4f68d

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

3sum/casentino.ts

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

climbing-stairs/casentino.ts

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

valid-anagram/casentino.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function isAnagram(s: string, t: string): boolean {
2+
if (s.length !== t.length) {
3+
return false;
4+
}
5+
const count = new Array(26).fill(0);
6+
for (let i = 0; i < s.length; i++) {
7+
count[s.charCodeAt(i) - 97]++;
8+
count[t.charCodeAt(i) - 97]--;
9+
}
10+
for (let c of count) {
11+
if (c !== 0) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
class TreeNode {
15+
val: number;
16+
left: TreeNode | null;
17+
right: TreeNode | null;
18+
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
19+
this.val = val === undefined ? 0 : val;
20+
this.left = left === undefined ? null : left;
21+
this.right = right === undefined ? null : right;
22+
}
23+
}
24+
25+
function isValidBST(root: TreeNode | null): boolean {
26+
function recursiveSearch(node: TreeNode | null, min: number | null, max: number | null) {
27+
if (!node) {
28+
return true;
29+
}
30+
if (min !== null && node.val <= min) {
31+
return false;
32+
}
33+
if (max !== null && node.val >= max) {
34+
return false;
35+
}
36+
return recursiveSearch(node.left, min, node.val) && recursiveSearch(node.right, node.val, max);
37+
}
38+
return recursiveSearch(root, null, null);
39+
}

0 commit comments

Comments
 (0)