Skip to content

Commit

Permalink
Merge pull request #797 from HerrineKim/main
Browse files Browse the repository at this point in the history
[HerrineKim] Week 3
  • Loading branch information
HerrineKim authored Dec 28, 2024
2 parents a2556d1 + 3060e1e commit 7682aad
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
30 changes: 30 additions & 0 deletions combination-sum/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -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;
};
18 changes: 18 additions & 0 deletions maximum-subarray/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -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;
};
22 changes: 22 additions & 0 deletions two-sum/HerrineKim.js
Original file line number Diff line number Diff line change
@@ -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);
}
};

0 comments on commit 7682aad

Please sign in to comment.