-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubsets-ii.py
38 lines (29 loc) · 1015 Bytes
/
subsets-ii.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
37
38
# Leetcode 90. Subsets II
#
# Link: https://leetcode.com/problems/subsets-ii/
# Difficulty: Medium
# Tags: BackTracking, Subsets
# Solution using backtracking
# Complexity:
# O(N*2^N) time | where N represent the number of elements in the input array
# O(N*2^N) space | where N represent the number of elements in the input array
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
nums.sort()
result = list()
subset = list()
def backtrack(i):
# Base case
if i >= len(nums):
result.append(subset.copy())
return
# Decision 1, all subsets that include nums[i]
subset.append(nums[i])
backtrack(i + 1)
subset.pop()
# Decision 1, all subsets that do not include nums[i]
while i + 1 < len(nums) and nums[i] == nums[i + 1]:
i += 1
backtrack(i + 1)
backtrack(0)
return result