Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/emahtab/combination-sum-ii
https://github.com/emahtab/combination-sum-ii
backtracking combinations leetcode
Last synced: 1 day ago
JSON representation
- Host: GitHub
- URL: https://github.com/emahtab/combination-sum-ii
- Owner: eMahtab
- Created: 2024-09-26T09:39:56.000Z (4 months ago)
- Default Branch: main
- Last Pushed: 2024-10-27T10:14:07.000Z (3 months ago)
- Last Synced: 2024-12-07T03:12:22.776Z (about 2 months ago)
- Topics: backtracking, combinations, leetcode
- Homepage:
- Size: 13.7 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Combination Sum II
https://leetcode.com/problems/combination-sum-ii
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target.
Each number in candidates may only be used once in the combination.
Note: The solution set must not contain duplicate combinations.
```
Example 1:Input: candidates = [10,1,2,7,6,1,5], target = 8
Output:
[
[1,1,6],
[1,2,5],
[1,7],
[2,6]
]Example 2:
Input: candidates = [2,5,2,1,2], target = 5
Output:
[
[1,2,2],
[5]
]
```### Constraints:
1. 1 <= candidates.length <= 100
2. 1 <= candidates[i] <= 50
3. 1 <= target <= 30## Approach :
This problem is similar to [Combination Sum](https://github.com/eMahtab/combination-sum) problem, with couple of conditions, a number n can only be used x times, where x is number of times n appears in candidates array. And we should not add duplicate combinations. To solve this problem we sort the candidates array and then find the combinations, we skip the processing if we encounter same number which is not the starting of the group of same numbers.## Implementation :
```java
class Solution {
public List> combinationSum2(int[] candidates, int target) {
List> result = new ArrayList<>();
if(candidates == null || candidates.length == 0)
return result;
Arrays.sort(candidates);
findCombination(candidates, target, 0, new ArrayList(), result);
return result;
}private void findCombination(int[] candidates, int target, int index, List combination,
List> result) {
if(target == 0) {
result.add(new ArrayList(combination));
return;
}
for(int i = index; i < candidates.length; i++) {
if(i > index && candidates[i] == candidates[i-1]) continue;
if(candidates[i] <= target) {
combination.add(candidates[i]);
if(target - candidates[i] >= 0)
findCombination(candidates, target - candidates[i], i+1, combination, result);
combination.remove(combination.size()-1);
}
}
}
}
```
## Time and Space Complexity
The time complexity of the combinationSum2 function is exponential because for each element in the candidates array, the function makes a recursive call.### Time Complexity = O(2^N)
### Space Complexity = O(N)
# References :