add: 230403-CN

This commit is contained in:
Eatswap 2023-04-03 23:47:24 +08:00
parent 169e6b33a2
commit 599532847a
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
1 changed files with 28 additions and 0 deletions

28
cpp/2304/230403-CN.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <algorithm>
#include <iterator>
#include <vector>
/**
* 1053. Previous Permutation With One Swap
* Given an array of positive integers arr (not necessarily distinct), return the lexicographically largest permutation that is smaller than arr, that can be made with exactly one swap. If it cannot be done, then return the same array.
* Note that a swap exchanges the positions of two numbers arr[i] and arr[j]
*/
class Solution {
public:
std::vector<int> prevPermOpt1(std::vector<int> arr) {
if (std::is_sorted(arr.begin(), arr.end()))
return arr;
const int n = arr.size();
for (int i = n - 2; i >= 0; --i) {
if (arr[i] <= arr[i + 1])
continue;
int j = n - 1;
for (; arr[j] >= arr[i] || arr[j] == arr[j - 1]; --j);
std::swap(arr[i], arr[j]);
return arr;
}
// Should not reach here
return arr;
}
};