Skip to content

[eunice-hong] WEEK 02 solutions #1222

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 3sum/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
function threeSum(nums: number[]): number[][] {
// Sort the array to make it easier to find triplets that sum to zero
nums.sort((a, b) => a - b);
const result: number[][] = [];

// Iterate through the array to find triplets that sum to zero
for (let i = 0; i < nums.length - 2; i++) {
// Skip duplicates
if (i > 0 && nums[i] === nums[i - 1]) continue;

// Use two pointers to find the other two numbers
let left = i + 1;
let right = nums.length - 1;

while (left < right) {
const sum = nums[i] + nums[left] + nums[right];
if (sum === 0) {
// Add the triplet to the result array
result.push([nums[i], nums[left], nums[right]]);
left++;
right--;
// Skip duplicates
while (left < right && nums[left] === nums[left - 1]) left++;
while (left < right && nums[right] === nums[right + 1]) right--;
} else if (sum < 0) {
// Move the left pointer to the right to increase the sum
left++;
} else {
// Move the right pointer to the left to decrease the sum
right--;
}
}
}

return result;
}
22 changes: 22 additions & 0 deletions climbing-stairs/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function climbStairs(n: number): number {
// memoization to store the results of the subproblems
const memo = new Map();

// if n is 1 or 2, return n
memo.set(1, 1);
memo.set(2, 2);

// recursive function to calculate the number of ways to climb the stairs
function climb(n: number, memo: Map<number, number>): number {
let result = memo.get(n);
if (result) {
return result;
} else {
result = climb(n - 1, memo) + climb(n - 2, memo);
memo.set(n, result);
return result;
}
}

return climb(n, memo);
}
37 changes: 37 additions & 0 deletions product-of-array-except-self/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
function productExceptSelf(nums: number[]): number[] {
// Check if there are zeros in the array
const zeroCount = nums.reduce((acc, num) => {
if (num === 0) {
return acc + 1;
} else {
return acc;
}
}, 0);

if (zeroCount === 0) {
// If there are no zeros, calculate the product of all numbers
const totalProduct = nums.reduce((acc, num) => num * acc, 1);
return nums.map((num) => {
return totalProduct / num;
});
} else if (zeroCount === 1) {
// If there is one zero, calculate the product of all numbers except the zero
const totalProduct = nums.reduce((acc, num) => {
if (num === 0) {
return acc;
} else {
return num * acc;
}
}, 1);
return nums.map((num) => {
if (num === 0) {
return totalProduct
} else {
return 0;
}
});
} else {
// If there are more than one zero, return an array of zeros
return nums.map((_) => 0);
}
};
22 changes: 22 additions & 0 deletions valid-anagram/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
function isAnagram(s: string, t: string): boolean {
// if the length of the two strings are not the same, return false
if (s.length !== t.length) return false;

// create a map of the characters in string s and t
const sMap = new Map();
const tMap = new Map();

// iterate through the strings and add the characters to the maps
for (let i = 0; i < s.length; i++) {
sMap.set(s[i], (sMap.get(s[i]) ?? 0) + 1);
tMap.set(t[i], (tMap.get(t[i]) ?? 0) + 1);
}

// if the values of the maps are not the same, return false
for (let char of sMap.keys()) {
if (sMap.get(char) !== tMap.get(char)) {
return false;
}
}
return true;
}
29 changes: 29 additions & 0 deletions validate-binary-search-tree/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class TreeNode {
val: number
left: TreeNode | null
right: TreeNode | null
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
this.val = (val===undefined ? 0 : val)
this.left = (left===undefined ? null : left)
this.right = (right===undefined ? null : right)
}
}

function isValidBST(root: TreeNode | null): boolean {
// Helper function to check if a tree is a valid binary search tree
function isValidTree(node: TreeNode | null, min: number | null, max: number | null): boolean {
// If the node is null, the tree is valid
if (node === null) return true;

// If the node's value is less than the minimum or greater than the maximum, the tree is not valid
if ((min !== null && node.val <= min) || (max !== null && node.val >= max)) {
return false;
}

// Recursively check the left and right subtrees
return isValidTree(node.left, min, node.val) && isValidTree(node.right, node.val, max);
}

// Check if the tree is a valid binary search tree
return isValidTree(root, null, null);
}