Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 5959fff

Browse files
committedMay 8, 2025·
feat: combination-sum
1 parent fd63601 commit 5959fff

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed
 

‎combination-sum/minji-go.java

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
/*
2-
Problem: https://leetcode.com/problems/combination-sum/
3-
Description: return a list of all unique combinations of candidates where the chosen numbers sum to target
4-
Concept: Array, Backtracking
5-
Time Complexity: O(Nᵀ), Runtime 2ms
6-
Space Complexity: O(T), Memory 44.88MB
7-
*/
8-
class Solution {
9-
public List<List<Integer>> answer = new ArrayList<>();
1+
/**
2+
* <a href="https://leetcode.com/problems/combination-sum/">week03-3.combination-sum</a>
3+
* <li>Description: return a list of all unique combinations of candidates where the chosen numbers sum to target </li>
4+
* <li>Topics: Array, Backtracking </li>
5+
* <li>Time Complexity: O(K^T), Runtime 2ms </li>
6+
* <li>Space Complexity: O(T), Memory 44.9MB </li>
7+
*/
108

9+
class Solution {
1110
public List<List<Integer>> combinationSum(int[] candidates, int target) {
11+
List<List<Integer>> combinations = new ArrayList<>();
1212
Arrays.sort(candidates);
13-
findCombination(candidates, target, new ArrayList<>(), 0);
14-
return answer;
13+
dfs(candidates, target, 0, new ArrayList<>(), combinations);
14+
return combinations;
1515
}
1616

17-
public void findCombination(int[] candidates, int target, List<Integer> combination, int idx) {
18-
if(target == 0) {
19-
answer.add(new ArrayList<>(combination));
17+
public void dfs(int[] candidates, int target, int index, List<Integer> combination, List<List<Integer>> combinations) {
18+
if (target == 0) {
19+
combinations.add(new ArrayList<>(combination));
2020
return;
2121
}
2222

23-
for(int i=idx; i<candidates.length; i++) {
24-
if(candidates[i] > target) break;
23+
for (int i = index; i < candidates.length; i++) {
24+
if (target - candidates[i] < 0) break;
2525

2626
combination.add(candidates[i]);
27-
findCombination(candidates, target-candidates[i], combination, i);
28-
combination.remove(combination.size()-1);
27+
dfs(candidates, target - candidates[i], i, combination, combinations);
28+
combination.remove(combination.size() - 1);
2929
}
3030
}
3131
}

0 commit comments

Comments
 (0)