/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var nextPermutation = function(nums) {
var i = nums.length - 2;
while (i > -1) {
if (nums[i] < nums[i+1]) {
break;
}
i--;
}
if (i > -1) {
var nextMin = {
index : i,
value: 101
};
for (var j = nums.length - 1; j > i ; j--) {
if (nums[j] > nums[i] && ((nums[j] - nums[i]) < nextMin.value)) {
nextMin.value = nums[j] - nums[i];
nextMin.index = j;
}
}
swapInArr(nums, i, nextMin.index);
}
reverseSubArray(nums, i + 1, nums.length - 1);
};
function swapInArr(arr, i, j) {
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
function reverseSubArray(arr, start , end) {
while(start < end) {
swapInArr(arr, start, end);
start++;
end--;
}
}