Skip to content

Commit

Permalink
add: combination-sum
Browse files Browse the repository at this point in the history
  • Loading branch information
HerrineKim committed Dec 27, 2024
1 parent 54506df commit 43b2831
Showing 1 changed file with 30 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;
};

0 comments on commit 43b2831

Please sign in to comment.