Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions combination-sum/ready-oun.java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍
DFS 풀이보다 더 깔끔한 것 같아서 참고해보겠습니다!
target에서 빼면서 0인지 체크하는 풀이가 더 좋은 것 같네요!
(저는 더해서 체크하느라..)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

감사합니다! 이 정도 난이도는 처음 풀어봐서 많이 어려웠는데 백트래킹에 대해서 공부할 수 있는 시간이었습니다 ㅎㅎ 머지하겠습니다 !

Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.util.*;
class Solution {
public List<List<Integer>> combinationSum(int[] candidates, int target) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> temp = new ArrayList<>();

backtrack(candidates, target, 0, temp, result);
return result;
}
private void backtrack(int[] candidates, int target, int start, List<Integer> temp, List<List<Integer>> result) {
if (target < 0) return;
if (target == 0) {
result.add(new ArrayList<>(temp)); // deep copy
return;
}

for (int i = start; i < candidates.length; i++) {
temp.add(candidates[i]);
backtrack(candidates, target - candidates[i], i, temp, result);
temp.remove(temp.size() -1);
}

}
}

/**
Return all unique combinations where the candidate num sum to target
- each num in cand[] can be used unlimited times
- order of num in comb does NOT matter

1. use backtracking to explore all possible comb
2. backtrack, if current sum > target
3. save comb, if current sum == target
4. avoid dupl -> only consider num from crnt idx onward (no going back)

Time: O(2^target)
Space: O(target)

Learned: Backtracking vs DFS
- DFS: search all paths deeply (no conditions, no rollback).
- Backtracking = DFS + decision making + undo step.
Explore, prune (if invalid), save (if valid), then undo last choice.

for (선택 가능한 숫자 하나씩) {
선택하고
target 줄이고 (목표 가까워짐)
재귀 호출로 다음 선택
실패하거나 성공하면 되돌리기 (백트래킹)
}

DFS just visits everything,
Backtracking visits only what’s promising — and turns back if not!
*/
Loading