← Home

90. Subsets II

LeetCode article · C++ solution
Website made by wuisabel-gif · Original C++ code by keineahnung2345
backtrackingC++Markdown
90

The trick here is to name the state correctly, then let the implementation follow. For 90. Subsets II, the solution in this repository is mainly a backtracking solution.

Guide

What?

Before optimizing anything, pin down what information is still useful after each move. Instead of trying to be clever immediately, read the code as a sequence of questions:

  • What state are we keeping?
  • How do we move from one state to the next?
  • When do we know the answer is already determined?

For this file, the main tools are: backtracking, greedy.

Guide

When?

Reach for this shape when a direct simulation would work logically but waste time revisiting the same information. The accepted code reduces that pressure by storing exactly the information that remains useful later.

The important function names to track are backtrack.

Guide

Why?

The point of the implementation is not to make the code longer. It is to avoid doing the same thinking twice.

  • Sorting is used to make local choices comparable, so the later scan does not have to rediscover order.
  • The two-dimensional vector is the memory of the solution: grid state, DP state, or adjacency shape.
  • The final return is not magic; it is the invariant after the loops or recursion have finished doing their accounting.

Guide

How?

Walk through the solution in this order:

  1. Read the setup variables first.
  2. Follow the main loop or recursive helper next.
  3. Watch where invalid states get skipped.
  4. Check which value survives to the return statement.

The most important competitive-programming habit here is to trust the invariant. Once the invariant is right, the loops become much less scary.

Guide

Complexity

  • Time: O(n) to O(n log n), depending on the dominant loop or data structure operation
  • Space: O(n) in the usual case for auxiliary containers or recursion

Guide

C++ Solution

Your submission

The accepted solution

solution.cpp
01//Runtime: 4 ms, faster than 99.58% of C++ online submissions for Subsets II.
02//Memory Usage: 8.2 MB, less than 100.00% of C++ online submissions for Subsets II.
03
04class Solution {
05public:
06    void backtrack(vector<vector<int>>& ans, vector<int>& subset, vector<int>& nums, int start){
07        ans.push_back(subset);
08        for(int i = start; i < nums.size(); i++){
09            /*
10            [1,4,4,4,4]
11            0
12            01
13            012
14            0123
15            01234
16            0124(equals 0123)
17            013(equals 012)
18            0134(equlas 0124)
19            014(equlas 013)
20            02(equals 01)
21            023(equals 013)
22            0234(equlas 0134)
23            1
24            12
25            123
26            1234
27            124(equlas 123)
28            13(equlas 12)
29            134(equals 124)
30            14(equals 13)
31            2(equals 1)
32            ...
33            3(equals 2)
34            ...
35            4(equals 3)
36            */
37            if(i > start && nums[i] == nums[i-1]) continue;
38            subset.push_back(nums[i]);
39            backtrack(ans, subset, nums, i+1);
40            subset.pop_back();
41        }
42    };
43    
44    vector<vector<int>> subsetsWithDup(vector<int>& nums) {
45        vector<vector<int>> ans;
46        vector<int> cur;
47        
48        //sorting here is important!
49        sort(nums.begin(), nums.end());
50        backtrack(ans, cur, nums, 0);
51        
52        return ans;
53    }
54};

Cost

Complexity

Time
O(n) to O(n log n), depending on the dominant loop or data structure operation
Dominated by the main traversal, recursion, or data-structure operations in the code.
Space
O(n) in the usual case for auxiliary containers or recursion
Auxiliary state plus the answer structure where the problem requires one.