diff --git a/combination-sum/HerrineKim.js b/combination-sum/HerrineKim.js new file mode 100644 index 000000000..4549f6f24 --- /dev/null +++ b/combination-sum/HerrineKim.js @@ -0,0 +1,30 @@ +// 시간 복잡도 : O(n^2) +// 공간 복잡도 : O(n) + +/** + * @param {number[]} candidates + * @param {number} target + * @return {number[][]} + */ + +var combinationSum = function(candidates, target) { + const result = []; + + const backtrack = (remaining, combo, start) => { + if (remaining === 0) { + result.push([...combo]); + return; + } + + for (let i = start; i < candidates.length; i++) { + if (candidates[i] <= remaining) { + combo.push(candidates[i]); + backtrack(remaining - candidates[i], combo, i); + combo.pop(); + } + } + }; + + backtrack(target, [], 0); + return result; +}; diff --git a/maximum-subarray/HerrineKim.js b/maximum-subarray/HerrineKim.js new file mode 100644 index 000000000..87ed8199c --- /dev/null +++ b/maximum-subarray/HerrineKim.js @@ -0,0 +1,18 @@ +// 시간 복잡도 : O(n) +// 공간 복잡도 : O(1) + +/** + * @param {number[]} nums + * @return {number} + */ +var maxSubArray = function(nums) { + let currentSum = nums[0]; + let maxSum = nums[0]; + + for (let i = 1; i < nums.length; i++) { + currentSum = Math.max(nums[i], currentSum + nums[i]); + maxSum = Math.max(maxSum, currentSum); + } + + return maxSum; +}; diff --git a/two-sum/HerrineKim.js b/two-sum/HerrineKim.js new file mode 100644 index 000000000..595c2898b --- /dev/null +++ b/two-sum/HerrineKim.js @@ -0,0 +1,22 @@ +// 시간 복잡도 : O(n) +// 공간 복잡도 : O(n) + +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + let numMap = new Map(); + + for (let i = 0; i < nums.length; i++) { + let complement = target - nums[i]; + + if (numMap.has(complement)) { + return [numMap.get(complement), i]; + } + + numMap.set(nums[i], i); + } +}; +