-
Notifications
You must be signed in to change notification settings - Fork 31
/
0039-combination-sum.py
34 lines (26 loc) · 1.55 KB
/
0039-combination-sum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
"""
Problem: LeetCode 39 - Combination Sum
Key Idea:
To find all unique combinations that sum up to a target value, we can use a backtracking approach. Starting from each candidate element, we explore all possible combinations by adding the element to the current combination and recursively searching for the remaining sum. If the sum becomes equal to the target, we add the current combination to the result. This process is repeated for each candidate element.
Time Complexity:
- In the worst case, each candidate element can be used multiple times to reach the target sum. Therefore, the time complexity is O(k * 2^n), where k is the average number of times each element can be used and n is the number of candidate elements.
Space Complexity:
- The space complexity is O(target), as the recursive call stack can go up to the target value.
"""
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
def backtrack(start, target, combination):
if target == 0:
result.append(
combination[:]
) # Append a copy of the current combination
return
for i in range(start, len(candidates)):
if candidates[i] > target:
continue # Skip if the candidate is too large
combination.append(candidates[i])
backtrack(i, target - candidates[i], combination)
combination.pop() # Backtrack
result = []
backtrack(0, target, [])
return result