Skip to content

[Helena] Week 12 solutions #190

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 3 commits into from
Jul 22, 2024
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
19 changes: 19 additions & 0 deletions jump-game/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
30 changes: 30 additions & 0 deletions longest-common-subsequence/yolophg.js
Original file line number Diff line number Diff line change
@@ -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);
};
38 changes: 38 additions & 0 deletions longest-increasing-subsequence/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
26 changes: 26 additions & 0 deletions maximum-subarray/yolophg.js
Original file line number Diff line number Diff line change
@@ -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;
};
18 changes: 18 additions & 0 deletions unique-paths/yolophg.js
Original file line number Diff line number Diff line change
@@ -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];
};