Let's make this one less mysterious. For 31. Next Permutation, the solution in this repository is mainly a greedy solution.
Guide
What?
We want to turn the problem statement into a smaller set of decisions the computer can repeat safely. 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: greedy.
Guide
When?
This is the kind of solution you want when the problem has structure hiding inside a messy-looking input. The accepted code reduces that pressure by storing exactly the information that remains useful later.
The important function names to track are nextPermutation.
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 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:
- Initialize the memory or helper structure.
- Process candidates in the order the invariant expects.
- Update the answer only when the current state is valid.
- Return the value that represents the fully processed input.
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), space: O(1)
- Space: O(n) in the usual case for auxiliary containers or recursion
Guide
C++ Solution
Your submission
The accepted solution
01//Runtime: 4 ms, faster than 92.92% of C++ online submissions for Next Permutation.
02//Memory Usage: 12.2 MB, less than 47.00% of C++ online submissions for Next Permutation.
03class Solution {
04public:
05 void nextPermutation(vector<int>& nums) {
06 int n = nums.size();
07 if(n <= 1) return;
08
09 for(int i = n-2; i >= 0; --i){
10 if(nums[i] < nums[i+1]){
11 //select the minidx s.t. nums[minidx] > nums[i] and is the smallest among nums[i+1:]
12 int minidx = i+1;
13 for(int j = i+1; j < n; ++j){
14 if(nums[j] > nums[i]){
15 minidx = (nums[j] < nums[minidx]) ? j : minidx;
16 }
17 }
18
19 //this is wrong!!
20 // auto it = min_element(nums.begin()+i+1, nums.end());
21
22 swap(nums[i], nums[minidx]);
23 sort(nums.begin()+i+1, nums.end());
24 return;
25 }
26 }
27
28 sort(nums.begin(), nums.end());
29 }
30};
31
32//Approach 2: Single Pass Approach
33//Runtime: 4 ms, faster than 92.92% of C++ online submissions for Next Permutation.
34//Memory Usage: 12.4 MB, less than 18.40% of C++ online submissions for Next Permutation.
35//time: O(N), space: O(1)
36class Solution {
37public:
38 void nextPermutation(vector<int>& nums) {
39 int n = nums.size();
40
41 int i = n-2;
42
43 while(i >= 0 && nums[i] >= nums[i+1]){
44 --i;
45 }
46
47 if(i >= 0){
48 //nums[i] < nums[i+1]
49 int j = n-1;
50
51 /*
52 find a j > i s.t. nums[j] > nums[i] and
53 is the smallest in nums[i+1:],
54 to do this, we find from the end,
55 because nums[i+1:] is in descending order
56 */
57 while(j >= 0 && nums[j] <= nums[i]){
58 --j;
59 }
60
61 swap(nums[i], nums[j]);
62
63 //after swapping, nums[i+1:] is still in descending order
64 }
65
66 reverse(nums.begin()+i+1, nums.end());
67 }
68};
Cost