-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNext Permutation.cpp
More file actions
45 lines (40 loc) · 1.4 KB
/
Copy pathNext Permutation.cpp
File metadata and controls
45 lines (40 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
// Problem: Rearrange numbers into the lexicographically next greater permutation.
// If not possible (array is in descending order), rearrange into the smallest permutation.
//
// Approach (Next Permutation Algorithm):
// 1. Find the "pivot" from the right where nums[i] < nums[i+1].
// 2. If no pivot exists, reverse the whole array (last → first permutation).
// 3. Otherwise, swap pivot with the rightmost element greater than it.
// 4. Reverse the suffix (elements to the right of pivot) to make it smallest.
//
// Time Complexity: O(n), Space Complexity: O(1)
class Solution {
public:
void nextPermutation(vector<int>& nums) {
int pivot = -1, n = nums.size();
// Step 1: Find pivot
for (int i = n - 2; i >= 0; i--) {
if (nums[i] < nums[i + 1]) {
pivot = i;
break;
}
}
// Step 2: If no pivot, reverse entire array
if (pivot == -1) {
reverse(nums.begin(), nums.end());
return;
}
// Step 3: Swap pivot with rightmost larger element
for (int i = n - 1; i > pivot; i--) {
if (nums[i] > nums[pivot]) {
swap(nums[i], nums[pivot]);
break;
}
}
// Step 4: Reverse suffix
int i = pivot + 1, j = n - 1;
while (i < j) {
swap(nums[i++], nums[j--]);
}
}
};