forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
}; |