-
Notifications
You must be signed in to change notification settings - Fork 1
/
subsets_ii.py
59 lines (47 loc) · 1.76 KB
/
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from typing import List
class Solution:
"""
Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
The solution set must not contain duplicate subsets. Return the solution in any order.
"""
def __init__(self):
self.res = None
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
"""
Given an integer array nums that may contain duplicates, return all possible subsets (the power set).
:param nums: The input integer array.
:type nums: List[int].
:return: All possible subsets without duplicates.
:rtype: List[List[int]].
The time complexity is O(N*2^N). Faster than 49.36% of solutions.
The space complexity is O(N) due to the recursive program stack. Less memory than 10.45% of solutions.
"""
self.res = []
nums.sort()
self.dfs(0, [], nums)
return self.res
def dfs(self, i: int, subset: List[int], nums: List[int]):
"""
A helper method to perform a Depth-First Search (DFS).
:param i: The current index.
:type i: Int.
:param subset: The current subset.
:type subset: List[int].
:param nums: The input integer array.
:type nums: List[int].
:return: NoneType.
:rtype: NoneType.
"""
# base case
if i >= len(nums):
self.res.append(subset.copy())
return
# decision to include nums[i]
subset.append(nums[i])
self.dfs(i + 1, subset, nums)
# decision to NOT include nums[i]
subset.pop()
# skip over duplicates
while i + 1 < len(nums) and nums[i + 1] == nums[i]:
i += 1
self.dfs(i + 1, subset, nums)