Skip to main content

Next Permutation

Problem

A permutation of an array of integers is an arrangement of its members into a sequence or linear order.

  • For example, for arr = [1,2,3], the following are considered permutations of arr: [1,2,3], [1,3,2], [3,1,2], [2,3,1].

The next permutation of an array of integers is the next lexicographically greater permutation of its integer. More formally, if all the permutations of the array are sorted in one container according to their lexicographical order, then the next permutation of that array is the permutation that follows it in the sorted container. If such arrangement is not possible, the array must be rearranged as the lowest possible order (i.e., sorted in ascending order).

  • For example, the next permutation of arr = [1,2,3] is [1,3,2].
  • Similarly, the next permutation of arr = [2,3,1] is [3,1,2].
  • While the next permutation of arr = [3,2,1] is [1,2,3] because [3,2,1] does not have a lexicographical larger rearrangement.

Given an array of integers nums, find the next permutation of nums.

The replacement must be in place and use only constant extra memory.

 

Example 1:

Input: nums = [1,2,3]
Output: [1,3,2]

Example 2:

Input: nums = [3,2,1]
Output: [1,2,3]

Example 3:

Input: nums = [1,1,5]
Output: [1,5,1]

 

Constraints:

  • 1 <= nums.length <= 100
  • 0 <= nums[i] <= 100

Solution

/**
* @param {number[]} nums
* @return {void} Do not return anything, modify nums in-place instead.
*/
var nextPermutation = function(nums) {
let i = nums.length - 2; // index directly before start of last non-increasing sequence
while (nums[i] >= nums[i + 1]) { // while non-increasing sequence
i--;
}

if (i !== -1) {
let j = nums.length - 1; // index of value closest and > nums[i]
while (nums[j] <= nums[i]) {
j--;
}
[nums[i], nums[j]] = [nums[j], nums[i]]; // swap values at index i and j
}
reverse(nums, i + 1);
};

// in-place reverse nums[l:nums.length]
var reverse = function(nums, l) {
let r = nums.length - 1;
while (l < r) {
[nums[l], nums[r]] = [nums[r], nums[l]];
l++;
r--;
}
};

We will implement a two pointers solution. First, observe that if we have a non-increasing sequence A =[an, ..., a2, a1], then this sequence must be the largest lexicographically (and the reverse would be the smallest lexicographically).

Now, consider the previous sequence, but we add B = [b1, b2, ..., bm] to the start (not necessarily sorted in any way), so we get the resulting sequence B + A = [b1, b2, ..., bm, an, ..., a2, a1]. Say bm <= an, since A is already the largest lexicographically, this must mean the change that needs to be made is bm. More specifically, bm needs to be swapped with the smallest value in A greater than it, say ai (ie. we now have [b1, b2, ..., ai, an, ..., bm, ..., a2, a1]).

Recall that A is sorted in descending order, so starting from the end of A and iterating backwards, the first value that is > bm is ai. Futhermore, observe that by swapping bm and ai, the new sequence of A, say A' = [an, ..., bm, ..., a2, a1], would still be sorted in descending order.

After the swap, we want A' to be the smallest lexicographically so that our sequence is the next permutation of B + A. The naive method is to just sort A'. However, remember that A' is sorted in descending order (ie. it is the largest lexicographically), so we can simply reverse A' so it becomes the smallest lexicographically. In some sense, this step is similar to having a carryover while incrementing by 1 (eg. the value after 19 is 19 + 1 = 20, where the tens digit increases due to the carryover, but the ones digit reverts to the smallest value)

Thus, we just need to find such a bm and implement the process described above (bm must be the first starting from the end of the provided permutation).