-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31NextPermutation.js
More file actions
48 lines (38 loc) · 912 Bytes
/
Copy path31NextPermutation.js
File metadata and controls
48 lines (38 loc) · 912 Bytes
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
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
// 1 2 5 4 3
// 1 2 3 4 5
// 1 3 2 4 5
//O(N)
const reverse = (arr, start, num) => {
let end = arr.length -1;
let i = start;
let swapIndex = null;
let swapValue = Infinity;
while(start <= end){
if(i < end) {
[arr[i], arr[end]] = [arr[end], arr[i]];
}
if(arr[end] > num && arr[end] <=swapValue){
swapValue = arr[end];
swapIndex = end;
}
end--;
i++;
}
return swapIndex;
}
var nextPermutation = function(nums) {
//iterate from the back to find first decreasing number
let i = nums.length - 1;
while(nums[i-1] >= nums[i]){
i--;
}
const swapIndex = reverse(nums,i,nums[i-1]);
if(swapIndex !== null) {
[nums[i-1], nums[swapIndex]] = [nums[swapIndex], nums[i-1]]
}
return nums;
};