-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
next_permutation.cc
42 lines (35 loc) · 1.46 KB
/
next_permutation.cc
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
#include <algorithm>
#include <vector>
#include "test_framework/generic_test.h"
using std::swap;
using std::vector;
vector<int> NextPermutation(vector<int> perm) {
// Find the first entry from the right that is smaller than the entry
// immediately after it.
auto inversion_point = is_sorted_until(rbegin(perm), rend(perm));
if (inversion_point == rend(perm)) {
// perm is sorted in decreasing order, so it's the last permutation.
return {};
}
// Swap the entry referenced by inversion_point with smallest entry
// appearing after inversion_point that is greater than the entry referenced
// by inversion_point:
//
// 1.) Find the smallest entry after inversion_point that's greater than the
// entry referenced by inversion_point. Since perm must be sorted in
// decreasing order after inversion_point, we can use a fast algorithm
// to find this entry.
auto least_upper_bound =
upper_bound(rbegin(perm), inversion_point, *inversion_point);
// 2.) Perform the swap.
iter_swap(inversion_point, least_upper_bound);
// Reverse the subarray that follows inversion_point.
reverse(rbegin(perm), inversion_point);
return perm;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"perm"};
return GenericTestMain(args, "next_permutation.cc", "next_permutation.tsv",
&NextPermutation, DefaultComparator{}, param_names);
}