This is one of those problems where the clean idea matters more than the amount of code. For 47. Permutations II, the solution in this repository is mainly a backtracking solution.
Guide
What?
The code is easier to read if we treat it as a controlled search through possible states. 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?
Use this approach when the hard part is not syntax, but deciding what must stay true after every update. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are backtrack, used.
Guide
Why?
The solution works because it narrows the problem until every update has a clear reason to exist.
- 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:
- Start from the smallest reliable state.
- Expand one legal move at a time.
- Cache, count, or merge information as soon as it becomes settled.
- Let the final stored value answer the original question.
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
01//Runtime: 16 ms, faster than 99.73% of C++ online submissions for Permutations II.
02//Memory Usage: 9 MB, less than 100.00% of C++ online submissions for Permutations II.
03
04class Solution {
05public:
06 void backtrack(vector<vector<int>>& perms, vector<int>& perm, vector<int>& nums, vector<bool>& used){
07 if(perm.size() == nums.size()){
08 perms.push_back(perm);
09 }else{
10 /*
11 [1,1,2]
12 012
13 021
14 102(x because nums[1] == nums[0] && !used[0] )
15 120(index 1 skipped)
16 201
17 210(x because nums[1] == nums[0] && !used[0])
18 */
19 for(int i = 0; i < nums.size(); i++){
20 if(used[i]) continue;
21 //nums[i] plays the role of nums[i-1]
22 if(i > 0 && nums[i] == nums[i-1] && !used[i-1]) continue;
23 perm.push_back(nums[i]);
24 used[i] = true;
25 backtrack(perms, perm, nums, used);
26 perm.pop_back();
27 used[i] = false;
28 }
29 }
30 };
31
32 vector<vector<int>> permuteUnique(vector<int>& nums) {
33 int N = nums.size();
34 vector<vector<int>> ans;
35 vector<int> perm;
36 vector<bool> used(N, false);
37
38 //sort is important!
39 sort(nums.begin(), nums.end());
40 backtrack(ans, perm, nums, used);
41
42 return ans;
43 }
44};
Cost