Skip to content

Commit

Permalink
Merge pull request #389 from kim-young/main
Browse files Browse the repository at this point in the history
[kimyoung] WEEK 03 Solutions
  • Loading branch information
kim-young authored Aug 30, 2024
2 parents 320f246 + e5d46b2 commit 93fe432
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 0 deletions.
12 changes: 12 additions & 0 deletions climbing-stairs/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// found a pattern where the result is the addition of two previous elements,
// but not sure if this is the expected answer
var climbStairs = function (n) {
let arr = new Array(n).fill(1);
for (let i = 2; i <= n; i++) {
arr[i] = arr[i - 2] + arr[i - 1];
}
return n === 1 ? 1 : arr[n];
};

// time - O(n) iterate up to n times
// space - O(n) creates an array upto n elements
24 changes: 24 additions & 0 deletions coin-change/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var coinChange = function (coins, amount) {
// create a dp array that stores the minimum amount of coins used for each amount leading up to the target amount
// fill with array with amount + 1 as a default
const dp = [0, ...new Array(amount).fill(amount + 1)];

// loop through the coins
for (const coin of coins) {
// for each amount, assess how the current coin can modify the existing value within the dp array by comparing the min value
for (let i = 1; i <= amount; i++) {
// only works if coin is less than or equal to the assessing amount
if (coin <= i) {
// min comparison
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
}
}
}
// check target amount in dp array to see if it's the default value
// if it's default value, it means coin combination cannot lead up to target amount
// if it's not default value, that is the minimum required coin change to lead up to target amount
return dp[amount] === amount + 1 ? -1 : dp[amount];
};

// time - O(a * c) loops through amount * loops through coin
// space - O(a) depends on the size of amount
29 changes: 29 additions & 0 deletions combination-sum/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var combinationSum = function (candidates, target) {
let results = [];

function helper(idx, curr, total) {
// base case - when total = target push into results
if (total === target) {
results.push([...curr]);
return;
}
// base exit case - when the index is greater or equal to candidates or the total is greater than target, exit
if (idx >= candidates.length || total > target) {
return;
}

// recursive case
// case where we include the current value without advancing the index
curr.push(candidates[idx]);
helper(idx, curr, total + candidates[idx]);
curr.pop()
// case where we advance the index
helper(idx + 1, curr, total);
}
helper(0, [], 0);

return results;
};

// time - O(2^n) backtracking
// space - O(1)
18 changes: 18 additions & 0 deletions product-of-array-except-self/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
var productExceptSelf = function (nums) {
let result = new Array(nums.length).fill(0); // create a result array of length num
let pre = 1, post = 1; // default to 1

for (let i = 0; i < nums.length; i++) { // fill the result array with prefix (multiplication of left values)
result[i] = pre;
pre *= nums[i];
}
for (let i = nums.length - 1; i >= 0; i--) { // multiply the postfix (multiplication of right values) to the result array in their corresponding index
result[i] *= post;
post *= nums[i];
}

return result;
};

// time - O(n) iterate through the nums array twice - 2n, remove constant which ends up to be n
// space - O(1) result array not part of space complexity
14 changes: 14 additions & 0 deletions two-sum/kimyoung.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
var twoSum = function (nums, target) {
let map = new Map(); // create a map to store the number and index of each element
for(let i = 0; i < nums.length; i++) {
let diff = target - nums[i];
if(map.has(diff)) { // once the differnece is found, return both index
return [i, map.get(diff)];
} else { // otherwise add to map
map.set(nums[i], i)
}
}
};

// time - O(n) at worst, iterate through the entire nums array
// space - O(n) at worst, map the entire nums array

0 comments on commit 93fe432

Please sign in to comment.