-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path47_Permutations_II.py
More file actions
35 lines (24 loc) · 922 Bytes
/
47_Permutations_II.py
File metadata and controls
35 lines (24 loc) · 922 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
29
30
31
32
33
34
35
# 1 Possible Solution
# 1. Backtracking
class Solution:
# Time: O(N*2^N), Space: O(N)
def permuteUnique(self, nums: List[int]) -> List[List[int]]:
# Stores the solution
result = []
# Stores the permutation at computation
permutation = []
# Equivalent of {n: 0 for n in nums}
count = Counter(nums)
def dfs():
if len(permutation) == len(nums):
result.append(permutation[:])
return
for number in count:
if count[number] > 0:
permutation.append(number)
count[number] -= 1
dfs()
count[number] += 1
permutation.pop()
dfs()
return result