|
| 1 | +# 0090. Subsets II |
| 2 | + |
| 3 | +* Difficulty: medium |
| 4 | +* Link: https://leetcode.com/problems/subsets-ii/ |
| 5 | +* Topics: Array-String, Backtracking |
| 6 | +* highlight: 只有在不 duplicate 的情況加上新的 num |
| 7 | + |
| 8 | +# Clarification |
| 9 | + |
| 10 | +1. Check the inputs and outputs |
| 11 | + - INPUT:List[int] |
| 12 | + - OUTPUT:List[List[int]] |
| 13 | + |
| 14 | +# Naive Solution |
| 15 | + |
| 16 | +<aside> |
| 17 | +💡 從最簡單的方法開始 easy solution → only speak out |
| 18 | + |
| 19 | +</aside> |
| 20 | + |
| 21 | +### Thought Process |
| 22 | + |
| 23 | +1. 算出每個 number 的數量 |
| 24 | +2. 真對每個 subset 加上不同數量的 number |
| 25 | +- Implement |
| 26 | + |
| 27 | + ```python |
| 28 | + class Solution: |
| 29 | + def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: |
| 30 | + result = [[]] |
| 31 | + num_map = {} |
| 32 | + for num in nums: |
| 33 | + num_map[num] = num_map.get(num, 0) + 1 |
| 34 | + for key, value in num_map.items(): |
| 35 | + for idx in range(len(result)): |
| 36 | + for val in range(1,value + 1): |
| 37 | + result.append(result[idx] + [key] * val) |
| 38 | + return result |
| 39 | + ``` |
| 40 | + |
| 41 | + |
| 42 | +### Complexity |
| 43 | + |
| 44 | +- Time complexity:$O(N+N^3)$ |
| 45 | +- Space complexity:$O(N)$ |
| 46 | + |
| 47 | +### Problems & Improvement |
| 48 | + |
| 49 | +- 有使用額外的 space 去存個數 |
| 50 | +- time complexity 是否可降到 $O(N^2)$ |
| 51 | + |
| 52 | +# Improvement |
| 53 | + |
| 54 | +### Thought Process |
| 55 | + |
| 56 | +1. 將 nums 排序 |
| 57 | +2. 當數字相同時,只新增於相同數字的 subset |
| 58 | + - 因為如果新增到其他的 subset 就會重複 |
| 59 | +3. 當數字不同時,則可以對 result 內的所有 subset 加上新的數字 |
| 60 | +- Implement |
| 61 | + |
| 62 | + ```python |
| 63 | + class Solution: |
| 64 | + def subsetsWithDup(self, nums: List[int]) -> List[List[int]]: |
| 65 | + result = [[]] |
| 66 | + curr = [] |
| 67 | + nums.sort() |
| 68 | + for idx, num in enumerate(nums): |
| 69 | + if idx > 0 and nums[idx - 1] == num: |
| 70 | + curr = [subset + [num] for subset in curr] |
| 71 | + else: |
| 72 | + curr = [subset + [num] for subset in result] |
| 73 | + result += curr |
| 74 | + |
| 75 | + return result |
| 76 | + ``` |
| 77 | + |
| 78 | + - curr: 還可以再增長的 subset |
| 79 | + - 當數字相同時,curr 就只考慮由前一輪的 curr 加上新的 num |
| 80 | + - 當數字不相同時,curr 就可以為全部的 result 加上新的 num |
| 81 | + |
| 82 | + ```jsx |
| 83 | + Example: [1, 2, 2, 3, 3] |
| 84 | + result: [[]] |
| 85 | + ==== idx: 0 ==== |
| 86 | + [] |
| 87 | + ---- |
| 88 | + [1] |
| 89 | + ==== idx: 1 ==== |
| 90 | + [] |
| 91 | + [1] |
| 92 | + ---- |
| 93 | + [2] |
| 94 | + [1, 2] |
| 95 | + ==== idx: 2 ==== |
| 96 | + [] |
| 97 | + [1] |
| 98 | + [2] |
| 99 | + [1, 2] |
| 100 | + ---- |
| 101 | + [2, 2] |
| 102 | + [1, 2, 2] |
| 103 | + ==== idx: 3 ==== |
| 104 | + [] |
| 105 | + [1] |
| 106 | + [2] |
| 107 | + [1, 2] |
| 108 | + [2, 2] |
| 109 | + [1, 2, 2] |
| 110 | + ---- |
| 111 | + [3] |
| 112 | + [1, 3] |
| 113 | + [2, 3] |
| 114 | + [1, 2, 3] |
| 115 | + [2, 2, 3] |
| 116 | + [1, 2, 2, 3] |
| 117 | + ==== idx: 4 ==== |
| 118 | + [] |
| 119 | + [1] |
| 120 | + [2] |
| 121 | + [1, 2] |
| 122 | + [2, 2] |
| 123 | + [1, 2, 2] |
| 124 | + ---- |
| 125 | + [3] |
| 126 | + [1, 3] |
| 127 | + [2, 3] |
| 128 | + [1, 2, 3] |
| 129 | + [2, 2, 3] |
| 130 | + [1, 2, 2, 3] |
| 131 | + ---- |
| 132 | + [3, 3] |
| 133 | + [1, 3, 3] |
| 134 | + [2, 3, 3] |
| 135 | + [1, 2, 3, 3] |
| 136 | + [2, 2, 3, 3] |
| 137 | + [1, 2, 2, 3, 3] |
| 138 | + ``` |
| 139 | + |
| 140 | + |
| 141 | +### Complexity |
| 142 | + |
| 143 | +- Time complexity: $O(N^2)$ |
| 144 | +- Space complexity:$O(1)$ |
0 commit comments