diff --git a/climbing-stairs/sunjae95.js b/climbing-stairs/sunjae95.js new file mode 100644 index 00000000..fdf42b19 --- /dev/null +++ b/climbing-stairs/sunjae95.js @@ -0,0 +1,18 @@ +/** + * @description + * brainstorming: + * Dynamic Programming + * + * time complexity: O(n) + * space complexity: O(n) + */ + +var climbStairs = function (n) { + const dp = Array.from({ length: n + 1 }, () => 0); + dp[1] = 1; + dp[2] = 2; + + for (let i = 3; i < n + 1; i++) dp[i] = dp[i - 1] + dp[i - 2]; + + return dp[n]; +}; diff --git a/coin-change/sunjae95.js b/coin-change/sunjae95.js new file mode 100644 index 00000000..b7dd75d6 --- /dev/null +++ b/coin-change/sunjae95.js @@ -0,0 +1,90 @@ +/** + * @description + * brainstorming: + * 1. asc sort + division calculate + * 2. bfs + memoization + * + * strategy: + * bfs + memoization + * + * reason: + * Tried with brainstorming 1 but test case is false + * + * time complexity: O(n^k) + * space complexity: O(n) + */ +class Node { + constructor(val) { + this.value = val; + this.next = null; + } +} + +class CustomQueue { + constructor() { + this.front = null; + this.rear = null; + this.size = 0; + } + + push(val) { + const node = new Node(val); + + if (this.size === 0) { + this.front = node; + this.rear = node; + } else { + this.rear.next = node; + this.rear = node; + } + + this.size++; + } + + pop() { + if (this.size === 0) return null; + const node = this.front; + this.front = this.front.next; + this.size--; + if (this.size === 0) this.rear = null; + + return node.value; + } +} + +var coinChange = function (coins, amount) { + const queue = new CustomQueue(); + const memoSet = new Set(); + + if (amount === 0) return 0; + + for (const coin of coins) { + if (amount === coin) return 1; + + queue.push(coin); + memoSet.add(coin); + } + + let count = 1; + + while (queue.size) { + count++; + let depthSize = queue.size; + + while (depthSize--) { + const sum = queue.pop(); + + for (const coin of coins) { + const nextSum = sum + coin; + + if (memoSet.has(nextSum)) continue; + if (amount === nextSum) return count; + if (amount > nextSum) queue.push(nextSum); + + memoSet.add(nextSum); + } + } + } + + return -1; +}; diff --git a/combination-sum/sunjae95.js b/combination-sum/sunjae95.js new file mode 100644 index 00000000..099fc035 --- /dev/null +++ b/combination-sum/sunjae95.js @@ -0,0 +1,27 @@ +/** + * @description + * brainstorming: + * dfs + * + * time complexity: O(n^k) + * space complexity: O(n) + */ +var combinationSum = function (candidates, target) { + const answer = []; + + const dfs = (array, sum, index) => { + if (sum > target) return; + if (sum === target) return answer.push(array); + + for (let i = index; i < candidates.length; i++) { + const nextArray = array.concat(candidates[i]); + const nextSum = sum + candidates[i]; + + dfs(nextArray, nextSum, i); + } + }; + + candidates.forEach((value, i) => dfs([value], value, i)); + + return answer; +}; diff --git a/product-of-array-except-self/sunjae95.js b/product-of-array-except-self/sunjae95.js new file mode 100644 index 00000000..09dfe7a0 --- /dev/null +++ b/product-of-array-except-self/sunjae95.js @@ -0,0 +1,29 @@ +/** + * @description + * brainstorming: + * recursive function + * + * time complexity: O(n) + * space complexity: O(n) + */ +var productExceptSelf = function (nums) { + const answer = Array.from({ length: nums.length }, () => 0); + + const search = (left, right, i) => { + if (i === nums.length - 1) { + answer[i] = left * right; + return nums[i]; + } + + const productLeft = left * nums[i]; + const productRight = search(productLeft, right, i + 1); + + answer[i] = left * productRight; + + return productRight * nums[i]; + }; + + search(1, 1, 0); + + return answer; +}; diff --git a/two-sum/sunjae95.js b/two-sum/sunjae95.js new file mode 100644 index 00000000..0adfd47b --- /dev/null +++ b/two-sum/sunjae95.js @@ -0,0 +1,47 @@ +/** + * @description + * brainstorming: + * 1. O(n^2) brute force + * 2. hash table + */ + +/** + * @description brainstorming 1 solve + * time complexity: O(n^2) + * space complexity: O(1) + */ +var twoSum = function (nums, target) { + for (let i = 0; i < nums.length; i++) { + for (let j = i + 1; j < nums.length; j++) { + if (nums[i] + nums[j] === target) return [i, j]; + } + } +}; + +/** + * @description brainstorming 2 solve + * time complexity: O(n^2) + * space complexity: O(n) + */ +var twoSum = function (nums, target) { + const map = new Map(); + + nums.forEach((num, index) => { + if (!map.get(num)) return map.set(num, [index]); + + map.set(num, map.get(num).concat(index)); + }); + + for (let i = 0; i < nums.length; i++) { + const rest = target - nums[i]; + + if (!map.get(rest)) continue; + + const indexList = map.get(rest); + for (let j = 0; j < indexList.length; j++) { + if (i === indexList[j]) continue; + + return [i, indexList[j]]; + } + } +};