-
Notifications
You must be signed in to change notification settings - Fork 0
/
next-permutation.js
48 lines (31 loc) · 1.28 KB
/
next-permutation.js
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
45
46
47
48
/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var nextPermutation = function(nums) {
if (nums.length <= 1) return;
let leftHandSwap;
// Loop through the provided numbers from right to left (excluding the first as we need something to compare it to)
for (let i = nums.length - 2; i >= 0; i--) {
// Check if this number is lower than the previous one (marks our left-hand swap)
if (nums[i] < nums[i + 1]) {
leftHandSwap = i;
break;
}
}
// Loop through the provided numbers from right to left
for (let i = nums.length - 1; i > leftHandSwap; i--) {
// If the number is bigger than the left-hand one
if (nums[i] > nums[leftHandSwap]) {
// Swap the numbers round
[nums[i], nums[leftHandSwap]] = [nums[leftHandSwap], nums[i]];
// Reverse the rest of the array
let chopped = nums.splice(leftHandSwap + 1);
chopped.sort((a, b) => a - b);
nums.push(...chopped);
return;
}
}
// Right-hand swap not found, return lowest permutation instead
nums.sort((a, b) => a - b);
};