From 3810e50ff0eb8b58d6e37dc970e012a61b83b24e Mon Sep 17 00:00:00 2001 From: Young Kim Date: Mon, 26 Aug 2024 22:45:04 -0400 Subject: [PATCH 1/9] Two Sum solution --- two-sum/kimyoung.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 two-sum/kimyoung.js diff --git a/two-sum/kimyoung.js b/two-sum/kimyoung.js new file mode 100644 index 000000000..11857e586 --- /dev/null +++ b/two-sum/kimyoung.js @@ -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 From 4e17a7390488a4632a675b70d9a5a3b9ac4fa71f Mon Sep 17 00:00:00 2001 From: Young Kim Date: Mon, 26 Aug 2024 22:52:37 -0400 Subject: [PATCH 2/9] Product of Array Except Self solution --- product-of-array-except-self/kimyoung.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 product-of-array-except-self/kimyoung.js diff --git a/product-of-array-except-self/kimyoung.js b/product-of-array-except-self/kimyoung.js new file mode 100644 index 000000000..7ddbf98f4 --- /dev/null +++ b/product-of-array-except-self/kimyoung.js @@ -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 \ No newline at end of file From 6dff87146e23b7b876ded65de9973c715758192a Mon Sep 17 00:00:00 2001 From: Young Kim Date: Mon, 26 Aug 2024 22:53:30 -0400 Subject: [PATCH 3/9] quick fix - add space on last line --- product-of-array-except-self/kimyoung.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/product-of-array-except-self/kimyoung.js b/product-of-array-except-self/kimyoung.js index 7ddbf98f4..b6016c61c 100644 --- a/product-of-array-except-self/kimyoung.js +++ b/product-of-array-except-self/kimyoung.js @@ -15,4 +15,4 @@ var productExceptSelf = function (nums) { }; // 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 \ No newline at end of file +// space - O(1) result array not part of space complexity From 0363257a6f4b9a2c1d9a8fb5810b280879bcc1c2 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Mon, 26 Aug 2024 23:21:39 -0400 Subject: [PATCH 4/9] Climbing Stairs solution --- climbing-stairs/kimyoung.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 climbing-stairs/kimyoung.js diff --git a/climbing-stairs/kimyoung.js b/climbing-stairs/kimyoung.js new file mode 100644 index 000000000..07e90dfcf --- /dev/null +++ b/climbing-stairs/kimyoung.js @@ -0,0 +1,11 @@ +// 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 \ No newline at end of file From f5536b5dba5e37ee8768d3b6adb81660dab7e75f Mon Sep 17 00:00:00 2001 From: Young Kim Date: Mon, 26 Aug 2024 23:23:28 -0400 Subject: [PATCH 5/9] quick fix - add end line break --- climbing-stairs/kimyoung.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/climbing-stairs/kimyoung.js b/climbing-stairs/kimyoung.js index 07e90dfcf..325e7afd1 100644 --- a/climbing-stairs/kimyoung.js +++ b/climbing-stairs/kimyoung.js @@ -8,4 +8,5 @@ var climbStairs = function (n) { return n === 1 ? 1 : arr[n]; }; -// time - O(n) iterate up to n times \ No newline at end of file +// time - O(n) iterate up to n times +// space - O(n) creates an array upto n elements From b4b98e94abd158d26e173d62df9da84e64db011e Mon Sep 17 00:00:00 2001 From: Young Kim Date: Tue, 27 Aug 2024 10:43:37 -0400 Subject: [PATCH 6/9] Combination Sum solution --- combination-sum/kimyoung.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 combination-sum/kimyoung.js diff --git a/combination-sum/kimyoung.js b/combination-sum/kimyoung.js new file mode 100644 index 000000000..292a12414 --- /dev/null +++ b/combination-sum/kimyoung.js @@ -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) \ No newline at end of file From 57b9908137c3d44451b571aa62739644c00833c4 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Tue, 27 Aug 2024 10:44:02 -0400 Subject: [PATCH 7/9] quick fix - add end line break --- combination-sum/kimyoung.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/combination-sum/kimyoung.js b/combination-sum/kimyoung.js index 292a12414..269444145 100644 --- a/combination-sum/kimyoung.js +++ b/combination-sum/kimyoung.js @@ -26,4 +26,4 @@ var combinationSum = function (candidates, target) { }; // time - O(2^n) backtracking -// space - O(1) \ No newline at end of file +// space - O(1) From f5cfc084379bda433ba26d53feff1a5ce2ffb485 Mon Sep 17 00:00:00 2001 From: Young Kim Date: Wed, 28 Aug 2024 19:27:52 -0400 Subject: [PATCH 8/9] Coin Change solution --- coin-change/kimyoung.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 coin-change/kimyoung.js diff --git a/coin-change/kimyoung.js b/coin-change/kimyoung.js new file mode 100644 index 000000000..8084582bf --- /dev/null +++ b/coin-change/kimyoung.js @@ -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 \ No newline at end of file From e5d46b2d83c01edc8ab256b902728fde5a4bef8e Mon Sep 17 00:00:00 2001 From: Young Kim Date: Wed, 28 Aug 2024 19:28:21 -0400 Subject: [PATCH 9/9] add end line break --- coin-change/kimyoung.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coin-change/kimyoung.js b/coin-change/kimyoung.js index 8084582bf..25bd04f53 100644 --- a/coin-change/kimyoung.js +++ b/coin-change/kimyoung.js @@ -21,4 +21,4 @@ var coinChange = function (coins, amount) { }; // time - O(a * c) loops through amount * loops through coin -// space - O(a) depends on the size of amount \ No newline at end of file +// space - O(a) depends on the size of amount