-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombinationSum.py
More file actions
27 lines (19 loc) · 816 Bytes
/
combinationSum.py
File metadata and controls
27 lines (19 loc) · 816 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
# problem: https://leetcode.com/problems/combination-sum/
# Runtime: 196 ms, faster than 14.67% of Python3 online submissions for Combination Sum.
# Memory Usage: 14 MB, less than 87.78% of Python3 online submissions for Combination Sum.
from typing import List
class Solution:
def dfs(self, nums, target, i, stack):
if target == 0:
self.sol.append(stack.copy())
if target < 0:
return
for j in range(i, len(nums)):
stack.append(nums[j])
self.dfs(nums, target - nums[j], j, stack)
stack.pop()
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
candidates.sort()
self.sol = []
self.dfs(candidates, target, 0, [])
return self.sol