class Solution {
public:
void nextPermutation(vector<int>& nums) {
int index = nums.size() - 1;
while (index >0 &&
nums[index] >= nums[index - 1]) {
index--;
}
if (index == 0) {
sort(nums.begin(), nums.end());
return;
}
int minVal = INT_MAX, minIndex = 0;
for (int i = nums.size() - 1; i >= index -1; i--) {
if (nums[i] > nums[index - 1] &&
minVal > nums[i] ){
minVal = nums[i];
minIndex = i;
}
}
int tmp = nums[index - 1];
nums[index - 1] = nums[minIndex];
nums[minIndex] = tmp;
sort(nums.begin() + index, nums.end());
}
};