diff --git a/climbing-stairs/kimyoung.js b/climbing-stairs/kimyoung.js new file mode 100644 index 000000000..325e7afd1 --- /dev/null +++ b/climbing-stairs/kimyoung.js @@ -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 diff --git a/coin-change/kimyoung.js b/coin-change/kimyoung.js new file mode 100644 index 000000000..25bd04f53 --- /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 diff --git a/combination-sum/kimyoung.js b/combination-sum/kimyoung.js new file mode 100644 index 000000000..269444145 --- /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) diff --git a/product-of-array-except-self/kimyoung.js b/product-of-array-except-self/kimyoung.js new file mode 100644 index 000000000..b6016c61c --- /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 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