-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathnext_permutation.js
More file actions
69 lines (65 loc) · 1.71 KB
/
next_permutation.js
File metadata and controls
69 lines (65 loc) · 1.71 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/**
* @author Anirudh Sharma
*
* Implement next permutation, which rearranges numbers into the lexicographically next greater
* permutation of numbers.
*
* If such an arrangement is not possible, it must rearrange it as the lowest possible order
* (i.e., sorted in ascending order).
*
* The replacement must be in place and use only constant extra memory.
*
* Constraints:
*
* 1 <= nums.length <= 100
* 0 <= nums[i] <= 100
*/
var nextPermutation = function(nums) {
// Length of the array
const n = nums.length;
// Index of the first element that is smaller than
// the element to its right.
let index = -1;
// Loop from right to left
for (let i = n - 1; i > 0; i--) {
if (nums[i] > nums[i - 1]) {
index = i - 1;
break;
}
}
// Base condition
if (index === -1) {
reverse(nums, 0, n - 1);
return nums;
}
let j = n - 1;
// Again swap from right to left to find first element
// that is greater than the above find element
for (let i = n - 1; i >= index + 1; i--) {
if (nums[i] > nums[index]) {
j = i;
break;
}
}
// Swap the elements
swap(nums, index, j);
// Reverse the elements from index + 1 to the nums.length
reverse(nums, index + 1, n - 1);
return nums;
};
const reverse = (nums, i, j) => {
while (i < j) {
swap(nums, i, j);
i++;
j--;
}
};
const swap = (nums, i, index) => {
const temp = nums[index];
nums[index] = nums[i];
nums[i] = temp;
};
console.log(nextPermutation([1, 2, 3]));
console.log(nextPermutation([3, 2, 1]));
console.log(nextPermutation([1, 1, 5]));
console.log(nextPermutation([1]));