-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0090-subsets-ii.py
More file actions
28 lines (22 loc) · 808 Bytes
/
0090-subsets-ii.py
File metadata and controls
28 lines (22 loc) · 808 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
28
# time complexit: O(2^n + nlogn)
# space complexity: O(n)
from collections import Counter
from typing import List
class Solution:
def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
result = []
nums.sort()
def backtrack(start: int, comb: List[int], counter: Counter):
if list(comb) not in result:
result.append(list(comb))
for i in range(start, len(nums)):
if counter[nums[i]] > 0:
counter[nums[i]] -= 1
comb.append(nums[i])
backtrack(i + 1, comb, counter)
comb.pop()
counter[nums[i]] += 1
backtrack(0, [], Counter(nums))
return result
nums = [1, 2, 2]
print(Solution().subsetsWithDup(nums))