-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcombination_sum.py
36 lines (30 loc) · 1.42 KB
/
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
35
36
'''
Question: https://leetcode.com/problems/combination-sum/
'''
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
''' Perform a DFS on the given list of candidates '''
res = []
self.dfs(candidates, target, [], res)
return res
# Helper function for DFS
def dfs(self, nums, target, curr_path, res):
# Base case - Return since target not an exact match
if (target < 0):
return
# If target exactly zero, we found a combination
if (target == 0):
# Add the current path to the result
res.append(curr_path)
# print(f'\nNew Path added -> {res}\n')
return
# Iterate over the list of nums from i to n
for i in range(len(nums)):
# print(f'Curr = {nums[i]}, Nums = {nums}, New Target = {target}, Path = {curr_path}, res = {res}')
# Pass 4 params to the DFS function for each element in 'nums'
# 1. New candidates will be 'i'th element onwards -> nums[i:]
# 2. Subtract current number from target -> 'target - nums[i]' before recursive call
# 3. Add the curr nums to the new potential path -> 'path+[nums[i]]'
# 4. Pass the result array
new_target = target - nums[i]
self.dfs(nums[i:], new_target, curr_path+[nums[i]], res)