Skip to content

[Helena] Week 11 solutions #181

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 1 commit into from
Jul 17, 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
40 changes: 40 additions & 0 deletions coin-change/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Time Complexity: O(amount * coins.length)
// Space Complexity: O(amount)

var coinChange = function (coins, amount) {
// if the amount is 0, no coins are needed
if (amount === 0) return 0;

// a queue to hold the current amount and coins used to reach that amount
let queue = [{ amount: 0, steps: 0 }];

// a set to keep track of visited amounts to avoid revisiting them
let visited = new Set();
visited.add(0);

// start the BFS loop
while (queue.length > 0) {
// dequeue the first element
let { amount: currentAmount, steps } = queue.shift();

// iterate through each coin
for (let coin of coins) {
// calculate the new amount by adding the coin to the current amount
let newAmount = currentAmount + coin;

// if the new amount equals the target amount, return the number of steps plus one
if (newAmount === amount) {
return steps + 1;
}

// if the new amount is less than the target amount and hasn't been visited, enqueue it
if (newAmount < amount && !visited.has(newAmount)) {
queue.push({ amount: newAmount, steps: steps + 1 });
visited.add(newAmount);
}
}
}

// if the loop completes without finding the target amount, return -1
return -1;
};
39 changes: 39 additions & 0 deletions decode-ways/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Time Complexity: O(n)
// Space Complexity: O(1)

var numDecodings = function (s) {
const n = s.length;

// if the string is empty or starts with '0', it cannot be decoded
if (n === 0 || s[0] === "0") return 0;

// initialize variables to store and decode up to the previous and the current index
// corresponds to dp[i-1]
let prev1 = 1;
// corresponds to dp[i-2]
let prev2 = 1;

// iterate through the string from the second character onwards
for (let i = 1; i < n; i++) {
let current = 0;

// single digit decoding
let oneDigit = parseInt(s.substring(i, i + 1));
if (oneDigit >= 1 && oneDigit <= 9) {
current += prev1;
}

// two digit decoding
let twoDigits = parseInt(s.substring(i - 1, i + 1));
if (twoDigits >= 10 && twoDigits <= 26) {
current += prev2;
}

// update prev2 and prev1 for the next iteration
prev2 = prev1;
prev1 = current;
}

// the way to decode the entire string, stored in prev1
return prev1;
};
24 changes: 24 additions & 0 deletions maximum-product-subarray/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Time Complexity: O(n)
// Space Complexity: O(1)

var maxProduct = function (nums) {
// the current maximum product, and the current minimum product
let maxProduct = nums[0];
let currMax = nums[0];
let currMin = nums[0];

// iterate through the array starting from the second element
for (let i = 1; i < nums.length; i++) {
let num = nums[i];

// update the current maximum and minimum products
currMax = Math.max(num, num * currMax);
currMin = Math.min(num, num * currMin);

// update the overall maximum product
maxProduct = Math.max(maxProduct, currMax);
}

// return the maximum product found
return maxProduct;
};
38 changes: 38 additions & 0 deletions palindromic-substrings/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// Time Complexity: O(n^2)
// Space Complexity: O(n^2)

var countSubstrings = function (s) {
const n = s.length;
// a 2D array to store palindrome information
let dp = Array.from(Array(n), () => Array(n).fill(false));
let count = 0;

// every single character is a palindrome
for (let i = 0; i < n; i++) {
dp[i][i] = true;
count++;
}

// check for palindromic substrings of length 2
for (let i = 0; i < n - 1; i++) {
if (s[i] === s[i + 1]) {
dp[i][i + 1] = true;
count++;
}
}

// check for palindromic substrings of length 3 and more
for (let len = 3; len <= n; len++) {
for (let i = 0; i < n - len + 1; i++) {
//eEnding index of the current substring
let j = i + len - 1;
if (s[i] === s[j] && dp[i + 1][j - 1]) {
dp[i][j] = true;
count++;
}
}
}

// return the total count
return count;
};
28 changes: 28 additions & 0 deletions word-break/yolophg.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Time Complexity: O(n^2 * m) m : the average length of the words in the dictionary
// Space Complexity: O(n)

var wordBreak = function (s, wordDict) {
const n = s.length;

// a dp array where dp[i] means s[0..i-1] can be segmented into words
let dp = Array(n + 1).fill(false);

// to segment an empty string
dp[0] = true;

for (let i = 1; i <= n; i++) {
// check every substring s[j..i-1]
for (let j = 0; j < i; j++) {
// if s[0..j-1] can be segmented and s[j..i-1] is a word
if (dp[j] && wordDict.includes(s.substring(j, i))) {
dp[i] = true;
// no need to check further, we found a valid segmentation
break;
}
``;
}
}

// whether s[0..n-1] can be segmented
return dp[n];
};