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.
[Sehwan] Week11 solution with JavaScript
- Loading branch information
Showing
5 changed files
with
111 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,20 @@ | ||
/** | ||
* @param {number[]} coins | ||
* @param {number} amount | ||
* @return {number} | ||
*/ | ||
var coinChange = function (coins, amount) { | ||
const dp = new Array(amount + 1).fill(amount + 1); | ||
dp[0] = 0; | ||
|
||
for (let i = 0; i <= amount; i++) { | ||
for (let coin of coins) { | ||
if (coin <= i) dp[i] = Math.min(dp[i - coin] + 1, dp[i]); | ||
} | ||
} | ||
|
||
return dp[amount] < amount + 1 ? dp[amount] : -1; | ||
}; | ||
|
||
// TC: O(amount*coins.length) | ||
// SC: O(amount) |
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,26 @@ | ||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var numDecodings = function (s) { | ||
// Edge case | ||
if (s.length === 0 || s[0] === "0") return 0; | ||
|
||
let dp = new Array(s.length + 1).fill(0); | ||
|
||
dp[0] = 1; | ||
dp[1] = 1; | ||
|
||
for (let i = 2; i <= s.length; i++) { | ||
let single = s[i - 1]; | ||
let double = s[i - 2] + s[i - 1]; | ||
|
||
if (single >= 1 && single <= 9) dp[i] += dp[i - 1]; | ||
if (double >= 10 && double <= 26) dp[i] += dp[i - 2]; | ||
} | ||
|
||
return dp[s.length]; | ||
}; | ||
|
||
// TC: O(n) | ||
// SC: O(n) |
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,16 @@ | ||
var maxProduct = function (nums) { | ||
let result = nums[0]; | ||
let max = 1, | ||
min = 1; | ||
|
||
for (let num of nums) { | ||
const candidates = [min * num, max * num, num]; | ||
min = Math.min(...candidates); | ||
max = Math.max(...candidates); | ||
result = Math.max(max, result); | ||
} | ||
return result; | ||
}; | ||
|
||
// TC: O(n) | ||
// SC: O(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,20 @@ | ||
var countSubstrings = function (s) { | ||
let count = 0; | ||
|
||
const isPalindrom = (start, end) => { | ||
while (start >= 0 && end < s.length && s[start] === s[end]) { | ||
start--; | ||
end++; | ||
count++; | ||
} | ||
}; | ||
|
||
for (let i = 0; i < s.length; i++) { | ||
isPalindrom(i, i); // odd number of str | ||
isPalindrom(i, i + 1); // even number of str | ||
} | ||
return count; | ||
}; | ||
|
||
// TC: O(n^2) | ||
// SC: O(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,29 @@ | ||
var wordBreak = function (s, wordDict) { | ||
let memo = {}; | ||
|
||
const dfs = (start) => { | ||
if (start in memo) return memo[start]; | ||
|
||
if (start === s.length) { | ||
memo[start] = true; | ||
return true; | ||
} | ||
|
||
for (let word of wordDict) { | ||
if (s.substring(start, start + word.length) === word) { | ||
if (dfs(start + word.length)) { | ||
memo[start] = true; | ||
return true; | ||
} | ||
} | ||
} | ||
memo[start] = false; | ||
return false; | ||
}; | ||
|
||
return dfs(0); | ||
}; | ||
|
||
// n = s.length | m = wordDict.length | L = maximum length of word in wordDict | ||
// TC: O(n*m*L) | ||
// SC: O(n) |