← Home

46. Permutations

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

This is one of those problems where the clean idea matters more than the amount of code. For 46. Permutations, the solution in this repository is mainly a backtracking solution.

Guide

What?

The first job is to translate the English prompt into state, transition, and stopping conditions. 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.

Guide

When?

This pattern shows up when the brute force version has too many repeated checks, too many possible branches, or too much bookkeeping to do by hand. 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.

  • 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. Start from the smallest reliable state.
  2. Expand one legal move at a time.
  3. Cache, count, or merge information as soon as it becomes settled.
  4. 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

solution.cpp
01//Runtime: 12 ms, faster than 64.52% of C++ online submissions for Permutations.
02//Memory Usage: 8.5 MB, less than 100.00% of C++ online submissions for Permutations.
03class Solution {
04public:
05    void backtrack(vector<vector<int>>& pers, vector<int>& per, vector<int>& nums){
06        if(per.size() == nums.size()){
07            pers.push_back(per);
08        }else{
09            for(int e : nums){
10                if(find(per.begin(), per.end(), e) != per.end()){
11                    continue;
12                }
13                per.push_back(e);
14                backtrack(pers, per, nums);
15                per.pop_back();
16            }
17        }
18    };
19    
20    vector<vector<int>> permute(vector<int>& nums) {
21        vector<vector<int>> pers;
22        vector<int> per;
23        
24        backtrack(pers, per, nums);
25        
26        return pers;
27    }
28};

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.