Skip to content

Commit 1c71d26

Browse files
committed
Add week 11 solutions
1 parent c00280e commit 1c71d26

File tree

5 files changed

+169
-0
lines changed

5 files changed

+169
-0
lines changed

coin-change/yolophg.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Time Complexity: O(amount * coins.length)
2+
// Space Complexity: O(amount)
3+
4+
var coinChange = function (coins, amount) {
5+
// if the amount is 0, no coins are needed
6+
if (amount === 0) return 0;
7+
8+
// a queue to hold the current amount and coins used to reach that amount
9+
let queue = [{ amount: 0, steps: 0 }];
10+
11+
// a set to keep track of visited amounts to avoid revisiting them
12+
let visited = new Set();
13+
visited.add(0);
14+
15+
// start the BFS loop
16+
while (queue.length > 0) {
17+
// dequeue the first element
18+
let { amount: currentAmount, steps } = queue.shift();
19+
20+
// iterate through each coin
21+
for (let coin of coins) {
22+
// calculate the new amount by adding the coin to the current amount
23+
let newAmount = currentAmount + coin;
24+
25+
// if the new amount equals the target amount, return the number of steps plus one
26+
if (newAmount === amount) {
27+
return steps + 1;
28+
}
29+
30+
// if the new amount is less than the target amount and hasn't been visited, enqueue it
31+
if (newAmount < amount && !visited.has(newAmount)) {
32+
queue.push({ amount: newAmount, steps: steps + 1 });
33+
visited.add(newAmount);
34+
}
35+
}
36+
}
37+
38+
// if the loop completes without finding the target amount, return -1
39+
return -1;
40+
};

decode-ways/yolophg.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
var numDecodings = function (s) {
5+
const n = s.length;
6+
7+
// if the string is empty or starts with '0', it cannot be decoded
8+
if (n === 0 || s[0] === "0") return 0;
9+
10+
// initialize variables to store and decode up to the previous and the current index
11+
// corresponds to dp[i-1]
12+
let prev1 = 1;
13+
// corresponds to dp[i-2]
14+
let prev2 = 1;
15+
16+
// iterate through the string from the second character onwards
17+
for (let i = 1; i < n; i++) {
18+
let current = 0;
19+
20+
// single digit decoding
21+
let oneDigit = parseInt(s.substring(i, i + 1));
22+
if (oneDigit >= 1 && oneDigit <= 9) {
23+
current += prev1;
24+
}
25+
26+
// two digit decoding
27+
let twoDigits = parseInt(s.substring(i - 1, i + 1));
28+
if (twoDigits >= 10 && twoDigits <= 26) {
29+
current += prev2;
30+
}
31+
32+
// update prev2 and prev1 for the next iteration
33+
prev2 = prev1;
34+
prev1 = current;
35+
}
36+
37+
// the way to decode the entire string, stored in prev1
38+
return prev1;
39+
};

maximum-product-subarray/yolophg.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Time Complexity: O(n)
2+
// Space Complexity: O(1)
3+
4+
var maxProduct = function (nums) {
5+
// the current maximum product, and the current minimum product
6+
let maxProduct = nums[0];
7+
let currMax = nums[0];
8+
let currMin = nums[0];
9+
10+
// iterate through the array starting from the second element
11+
for (let i = 1; i < nums.length; i++) {
12+
let num = nums[i];
13+
14+
// update the current maximum and minimum products
15+
currMax = Math.max(num, num * currMax);
16+
currMin = Math.min(num, num * currMin);
17+
18+
// update the overall maximum product
19+
maxProduct = Math.max(maxProduct, currMax);
20+
}
21+
22+
// return the maximum product found
23+
return maxProduct;
24+
};

palindromic-substrings/yolophg.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Time Complexity: O(n^2)
2+
// Space Complexity: O(n^2)
3+
4+
var countSubstrings = function (s) {
5+
const n = s.length;
6+
// a 2D array to store palindrome information
7+
let dp = Array.from(Array(n), () => Array(n).fill(false));
8+
let count = 0;
9+
10+
// every single character is a palindrome
11+
for (let i = 0; i < n; i++) {
12+
dp[i][i] = true;
13+
count++;
14+
}
15+
16+
// check for palindromic substrings of length 2
17+
for (let i = 0; i < n - 1; i++) {
18+
if (s[i] === s[i + 1]) {
19+
dp[i][i + 1] = true;
20+
count++;
21+
}
22+
}
23+
24+
// check for palindromic substrings of length 3 and more
25+
for (let len = 3; len <= n; len++) {
26+
for (let i = 0; i < n - len + 1; i++) {
27+
//eEnding index of the current substring
28+
let j = i + len - 1;
29+
if (s[i] === s[j] && dp[i + 1][j - 1]) {
30+
dp[i][j] = true;
31+
count++;
32+
}
33+
}
34+
}
35+
36+
// return the total count
37+
return count;
38+
};

word-break/yolophg.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Time Complexity: O(n^2 * m) m : the average length of the words in the dictionary
2+
// Space Complexity: O(n)
3+
4+
var wordBreak = function (s, wordDict) {
5+
const n = s.length;
6+
7+
// a dp array where dp[i] means s[0..i-1] can be segmented into words
8+
let dp = Array(n + 1).fill(false);
9+
10+
// to segment an empty string
11+
dp[0] = true;
12+
13+
for (let i = 1; i <= n; i++) {
14+
// check every substring s[j..i-1]
15+
for (let j = 0; j < i; j++) {
16+
// if s[0..j-1] can be segmented and s[j..i-1] is a word
17+
if (dp[j] && wordDict.includes(s.substring(j, i))) {
18+
dp[i] = true;
19+
// no need to check further, we found a valid segmentation
20+
break;
21+
}
22+
``;
23+
}
24+
}
25+
26+
// whether s[0..n-1] can be segmented
27+
return dp[n];
28+
};

0 commit comments

Comments
 (0)