Skip to content

[eunice-hong] WEEK 4 Solutions #1369

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 6 commits into from
Apr 26, 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
24 changes: 24 additions & 0 deletions coin-change/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

/**
*
* Time Complexity: O(n * amount)
* Space Complexity: O(amount)
*/
function coinChange(coins: number[], amount: number): number {
// Initialize dp array with Infinity
const dp = new Array(amount + 1).fill(Infinity);
// Base case: 0 coins needed to make amount 0
dp[0] = 0;

// Iterate through each coin
for (let coin of coins) {
// Update dp array for each amount from coin to amount
for (let i = coin; i <= amount; i++) {
// Choose the minimum between current dp[i] and dp[i - coin] + 1
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}

// If dp[amount] is still Infinity, it means it's not possible to make the amount
return dp[amount] === Infinity ? -1 : dp[amount];
}
15 changes: 15 additions & 0 deletions find-minimum-in-rotated-sorted-array/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

/**
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
function findMin(nums: number[]): number {
let index = 0;
// find the index of where the value is greater than the next value
while (nums[index] < nums[(index + 1) % nums.length]) {
index++;
}
// return the next value
return nums[(index + 1) % nums.length]
};
31 changes: 31 additions & 0 deletions maximum-depth-of-binary-tree/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Definition for a binary tree node.
* 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)
* }
* }
*/

/**
*
* Time Complexity: O(n)
* Space Complexity: O(n)
*/
function maxDepth(root: TreeNode | null): number {
if (!root) {
// tree is empty
return 0
} else if (!root.left && !root.right) {
// tree has no children
return 1
} else {
// tree has at least one child
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right))
}
};
34 changes: 34 additions & 0 deletions merge-two-sorted-lists/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
/**
*
* Time Complexity: O(n + m)
* Space Complexity: O(n + m)
*/
function mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
if (!list1 && !list2) {
// both lists are empty
return null
} else if (!list1) {
// list1 is empty
return list2
} else if (!list2) {
// list2 is empty
return list1
} else if (list1.val <= list2.val) {
// list1's current node is smaller
return new ListNode(list1.val, mergeTwoLists(list1.next, list2))
} else {
// list2's current node is smaller
return new ListNode(list2.val, mergeTwoLists(list1, list2.next))
}
};
45 changes: 45 additions & 0 deletions word-search/eunice-hong.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@

/**
* Time Complexity: O(m * n * 4^k)
* Space Complexity: O(m * n)
*/
function exist(board: string[][], word: string): boolean {
// Initialize the board dimensions
const m = board.length;
const n = board[0].length;

const dfs = (i: number, j: number, index: number): boolean => {
// If we've matched all characters, return true
if (index === word.length) return true;
// If out of bounds or current character doesn't match, return false
if (i < 0 || i >= m || j < 0 || j >= n || board[i][j] !== word[index]) return false;

// Temporarily mark the current cell as visited
const temp = board[i][j];
board[i][j] = '#';

// Explore all four possible directions
const found =
dfs(i + 1, j, index + 1) ||
dfs(i - 1, j, index + 1) ||
dfs(i, j + 1, index + 1) ||
dfs(i, j - 1, index + 1);

// Unmark the current cell (backtracking)
board[i][j] = temp;

// Return true if we found the word
return found;
};

// Iterate through each cell in the board
for (let i = 0; i < m; i++) {
for (let j = 0; j < n; j++) {
// Start DFS from each cell
if (dfs(i, j, 0)) return true;
}
}

// If we didn't find the word, return false
return false;
}