Skip to content

[Sehwan] Week11 solution with JavaScript #177

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 2 commits into from
Jul 14, 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
20 changes: 20 additions & 0 deletions coin-change/nhistory.js
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 (const 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)
26 changes: 26 additions & 0 deletions decode-ways/nhistory.js
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)
16 changes: 16 additions & 0 deletions maximum-product-subarray/nhistory.js
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 (const 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)
20 changes: 20 additions & 0 deletions palindromic-substrings/nhistory.js
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)
29 changes: 29 additions & 0 deletions word-break/nhistory.js
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 (const 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)