1. Combination Sum:
This problem asks you to find all unique combinations of numbers from a list candidates that add up to a target sum. Each number can be used unlimited times.
Example:
Input:
candidates = [2, 3, 6, 7], target = 7
Output:
[[2, 2, 3], [7]]
How to Solve:
1.Use backtracking to get all possible combinations.
2.For each number, decide whether to include it in the current combination or skip it.
3.Stop moving further if the current sum more than target.
4.Use recursion to build the combination step by step.
2.Combination Sum II:
Example:
Input:
candidates = [10, 1, 2, 7, 6, 1, 5], target = 8
Output:
[[1, 1, 6], [1, 2, 5], [1, 7], [2, 6]]
How to Solve:
- Sort the candidates list to handle duplicates easily.
- Use backtracking similar to Combination Sum but: Skip duplicates; if the same number has already been considered for the current position.Move to the next index instead of reusing the same number.
Top comments (0)