Skip to content

Commit 8dc292b

Browse files
committed
combination-sum 초기 조건 for문으로 주던 방식에서 단일 라인으로 주는 방식으로 변경
1 parent d238880 commit 8dc292b

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

combination-sum/highball.js

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,24 @@ const combinationSum = function (candidates, target) {
55
const dfs = function (candidate, sum) {
66
if (sum > target) return;
77

8-
path.push(candidate);
8+
if (sum !== 0) path.push(candidate);
99

1010
if (sum === target) {
11-
result.push([...path]); //그냥 path 넣어주면 뒤에서 path 바뀔 때 result에 들어간 path도 같이 바뀜
11+
result.push([...path]);
1212
path.pop();
1313
return;
1414
}
1515

1616
for (let i = 0; i < candidates.length; i++) {
17-
if (candidates[i] >= candidate)
18-
//작은 애들 더해주는 건 이미 앞에서 했을 것이므로 큰 애들만 더해주면 됨
19-
dfs(candidates[i], sum + candidates[i]);
17+
if (candidates[i] >= candidate) dfs(candidates[i], sum + candidates[i]);
2018
}
2119

2220
path.pop();
2321
};
2422

25-
for (let i = 0; i < candidates.length; i++) {
26-
//맨 처음에는 candidate이 없기 때문에 초기 조건 세팅할 때 candidate 넣어줘야 함
27-
dfs(candidates[i], candidates[i]);
28-
}
23+
dfs(0, 0);
2924

3025
return result;
3126
};
27+
28+
console.log(combinationSum([2, 3, 5], 8));

0 commit comments

Comments
 (0)