Subsets II
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.
Example 1:
Input: nums = [1,2,2]
Output: [[],[1],[1,2],[1,2,2],[2],[2,2]]
Example 2:
Input: nums = [0]
Output: [[],[0]]
Constraints:
1 <= nums.length <= 10
-10 <= nums[i] <= 10
Solution
This question is an advanced version of Subsets, the only difference is that the input may contain duplicate elements.
So we can use the same approach as in Subsets to find the subsets of nums
. However, we still need to deduplicate the repeated elements.
In Deduplication we learned that we can deduplicate by sorting the candidates and avoiding using a duplicate candidate that we have not used previously, we will do the same here.
We wish to fill in the logics for backtracking1 template.
is_leaf
: all the paths is a subset ofnums
get_edges
: the potential edges are the numbers fromnums[start_index:]
or an empty edge that concludes the subsetis_valid
: check whether the candidatenums[i]
is the first appearance of that element in the current function call, that is,i > start_index and nums[i] == nums[i-1]
istrue
whennums[i]
is a duplicate, andfalse
otherwise.
Since at every node, a special "edge" is to "close" the subset, we can add a copy of path
to ans
regardless of the value of other states.
Then, when we get_edges
we can consider only the numbers from nums[start_index:]
, as we have visited nums[0:start_index]
.
Implementation
1def subsetsWithDup(self, nums: List[int]) -> List[List[int]]:
2 def dfs(start_index, path):
3 ans.append(path[:]) # add a copy of the path to the result
4 for i in range(start_index, len(nums)):
5 # prune if needed
6 if i > start_index and nums[i] == nums[i-1]: # avoid duplicates
7 continue
8 path.append(nums[i])
9 dfs(i + 1, path)
10 path.pop()
11
12 ans = []
13 nums.sort()
14 dfs(0, [])
15 return ans
What is the best way of checking if an element exists in a sorted array once in terms of time complexity? Select the best that applies.
Which data structure is used in a depth first search?
Solution Implementation
Which two pointer technique does Quick Sort use?
A person thinks of a number between 1 and 1000. You may ask any number questions to them, provided that the question can be answered with either "yes" or "no".
What is the minimum number of questions you needed to ask so that you are guaranteed to know the number that the person is thinking?
Recommended Readings
Top Patterns to Conquer the Technical Coding Interview Should the written word bore you fear not A delightful video alternative awaits iframe width 560 height 315 src https www youtube com embed LW8Io6IPYHw title YouTube video player frameborder 0 allow accelerometer autoplay clipboard write encrypted media gyroscope picture in picture
Recursion Recursion is one of the most important concepts in computer science Simply speaking recursion is the process of a function calling itself Using a real life analogy imagine a scenario where you invite your friends to lunch https algomonster s3 us east 2 amazonaws com recursion jpg You first
Runtime Overview When learning about algorithms and data structures you'll frequently encounter the term time complexity This concept is fundamental in computer science and offers insights into how long an algorithm takes to complete given a certain input size What is Time Complexity Time complexity represents the amount of time
Got a question? Ask the Teaching Assistant anything you don't understand.
Still not clear? Ask in the Forum, Discord or Submit the part you don't understand to our editors.