Skip to content

Commit e666b75

Browse files
committed
combination-sum 풀이를 11부터 빼서 0되는 path 찾는 알고리즘에서 반대로 바꿈
- 즉 0부터 더해서 11 되는 알고리즘으로 바꿈
1 parent e8cef85 commit e666b75

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

combination-sum/highball.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,29 @@ const combinationSum = function (candidates, target) {
22
const result = [];
33
const path = [];
44

5-
const dfs = function (candidate, residual) {
6-
if (residual < 0) return;
5+
const dfs = function (candidate, sum) {
6+
if (sum > target) return;
77

88
path.push(candidate);
99

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

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

2222
path.pop();
2323
};
2424

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

3030
return result;

0 commit comments

Comments
 (0)