From f1e7633d19be92fb8b28cd13fb2c41066e37b63a Mon Sep 17 00:00:00 2001 From: helenapark Date: Wed, 17 Jul 2024 16:06:36 -0400 Subject: [PATCH 1/3] Add week 12 solutions: longestCommonSubsequence, maximumSubarray --- longest-common-subsequence/yolophg.js | 30 +++++++++++++++++++++++++++ maximum-subarray/yolophg.js | 26 +++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 longest-common-subsequence/yolophg.js create mode 100644 maximum-subarray/yolophg.js diff --git a/longest-common-subsequence/yolophg.js b/longest-common-subsequence/yolophg.js new file mode 100644 index 000000000..38a56f3f1 --- /dev/null +++ b/longest-common-subsequence/yolophg.js @@ -0,0 +1,30 @@ +// Time Complexity: O(m * n) m : the length of text1, n : the length of text2 +// Space Complexity: O(m * n) + +var longestCommonSubsequence = function (text1, text2) { + // a memoization array to store results + const memo = Array(text1.length) + .fill(null) + .map(() => Array(text2.length).fill(null)); + + // helper function that uses recursion and memoization + function lcsHelper(i, j) { + // if either string is exhausted, return 0 + if (i === text1.length || j === text2.length) { + return 0; + } + + // if characters match, move diagonally and add 1 to the result + if (text1[i] === text2[j]) { + memo[i][j] = 1 + lcsHelper(i + 1, j + 1); + } else { + // if characters don't match, take the maximum result from moving right or down + memo[i][j] = Math.max(lcsHelper(i + 1, j), lcsHelper(i, j + 1)); + } + + return memo[i][j]; + } + + // start the recursion from the beginning of both strings + return lcsHelper(0, 0); +}; diff --git a/maximum-subarray/yolophg.js b/maximum-subarray/yolophg.js new file mode 100644 index 000000000..2eb086e60 --- /dev/null +++ b/maximum-subarray/yolophg.js @@ -0,0 +1,26 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) + +var maxSubArray = function (nums) { + let maxSum = nums[0]; + let currentSum = 0; + + // iterate through the array + for (let i = 0; i < nums.length; i++) { + // if currentSum is negative, reset it to 0 + if (currentSum < 0) { + currentSum = 0; + } + + // add the current element to currentSum + currentSum += nums[i]; + + // update maxSum if currentSum is greater + if (currentSum > maxSum) { + maxSum = currentSum; + } + } + + // return the maximum subarray sum + return maxSum; +}; From 374b58ef62d5d84429f0822de0d907ac46ba5d8b Mon Sep 17 00:00:00 2001 From: helenapark Date: Fri, 19 Jul 2024 02:29:04 -0400 Subject: [PATCH 2/3] Add week 12 solutions: longestIncreasingSubsequence, uniquePaths --- longest-increasing-subsequence/yolophg.js | 38 +++++++++++++++++++++++ unique-paths/yolophg.js | 18 +++++++++++ 2 files changed, 56 insertions(+) create mode 100644 longest-increasing-subsequence/yolophg.js create mode 100644 unique-paths/yolophg.js diff --git a/longest-increasing-subsequence/yolophg.js b/longest-increasing-subsequence/yolophg.js new file mode 100644 index 000000000..c14a04e5f --- /dev/null +++ b/longest-increasing-subsequence/yolophg.js @@ -0,0 +1,38 @@ +// Time Complexity: O(n log n) +// Space Complexity: O(n) + +var lengthOfLIS = function (nums) { + // array to store the smallest tail of all increasing subsequences of various lengths + let subsequence = []; + + for (let num of nums) { + // iterate through each number in the input array + let left = 0, + right = subsequence.length; + + // use binary search to find the position of the current number in the subsequence array + while (left < right) { + // calculate the middle index + let mid = Math.floor((left + right) / 2); + if (subsequence[mid] < num) { + // move the left boundary to the right + left = mid + 1; + } else { + // move the right boundary to the left + right = mid; + } + } + + // if left is equal to the length of subsequence, it means num is greater than any element in subsequence + if (left === subsequence.length) { + // append num to the end of the subsequence array + subsequence.push(num); + } else { + // replace the element at the found position with num + subsequence[left] = num; + } + } + + // the length of the subsequence array is the length of the longest increasing subsequence + return subsequence.length; +}; diff --git a/unique-paths/yolophg.js b/unique-paths/yolophg.js new file mode 100644 index 000000000..61b482172 --- /dev/null +++ b/unique-paths/yolophg.js @@ -0,0 +1,18 @@ +// Time Complexity: O(m * n) m : the number of rows, n : the number of columns +// Space Complexity: O(m * n) + +var uniquePaths = function (m, n) { + // dp[i][j] will store the number of unique paths to reach the cell (i, j) + let dp = Array.from({ length: m }, () => Array(n).fill(1)); + + // iterate through the grid starting from (1, 1) + for (let i = 1; i < m; i++) { + for (let j = 1; j < n; j++) { + // to reach (i-1, j) and (i, j-1) because the robot can only move down or right + dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; + } + } + + // the value at the bottom-right corner of the grid is the number of unique paths + return dp[m - 1][n - 1]; +}; From 77c128f9990e29c2813172b348e3209720f08dea Mon Sep 17 00:00:00 2001 From: helenapark Date: Sat, 20 Jul 2024 23:43:52 -0400 Subject: [PATCH 3/3] Add week 12 solutions: jumpGame --- jump-game/yolophg.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 jump-game/yolophg.js diff --git a/jump-game/yolophg.js b/jump-game/yolophg.js new file mode 100644 index 000000000..31975c05a --- /dev/null +++ b/jump-game/yolophg.js @@ -0,0 +1,19 @@ +// Time Complexity: O(n) +// Space Complexity: O(1) + +var canJump = function (nums) { + // start from the last position + let lastPos = nums.length - 1; + + // traverse the array from the end to the start + for (let i = nums.length - 2; i >= 0; i--) { + // if can jump from the current position to the lastPos or beyond + if (i + nums[i] >= lastPos) { + // move the lastPos to the current position + lastPos = i; + } + } + + // if have moved lastPos to the start, can reach the end + return lastPos === 0; +};