-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCombinationsSum.py
More file actions
27 lines (22 loc) · 832 Bytes
/
CombinationsSum.py
File metadata and controls
27 lines (22 loc) · 832 Bytes
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
# Question link - https://leetcode.com/problems/combination-sum/description/?envType=study-plan-v2&envId=top-interview-150
class Solution:
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
# This uses Brute Forced approch
res = []
# Recursive function
def dfs(i, cur , total):
# Basecase
if total == target:
res.append(cur.copy())
return
if i >= len(candidates) or total > target:
return
# We have two path - 1st include 2
cur.append((candidates[i]))
dfs(i, cur , total + candidates[i])
#2nd is not include 2
cur.pop()
dfs(i+1, cur , total)
# Call
dfs(0 , [] , 0)
return res