Question from Leetcode.
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 all the permutations ofarr
:[1,2,3], [1,3,2], [2, 1, 3], [2, 3, 1], [3,1,2], [3,2,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.
What are they asking?
Let’s break down the question. Leetcode 31(Next Permutation) is asking for the next permutation of an array of integers. A permutation is an arrangement of the members of an array into a sequence. The next permutation is the next lexicographically greater permutation of the array. This means that if 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 an arrangement is not possible, the array must be rearranged as the lowest possible order (i.e. sorted in ascending order).
Solving the problem
There are a few ways to solve this, we will do so in JavaScript & Java:
There are a few solutions to solving Leetcode 31 Next Permutation. One solution is to find the next largest permutation by reversing the order of the elements from the end of the array. Another solution is to find the next smallest permutation by swapping the first and second elements, then reversing the order of the elements from the end of the array.
The most optimal way would be to find the next largest permutation by reversing the order of the elements from the end of the array.
Here is the code in JavaScript:
function nextPermutation(nums) { // Start from the second last element in the array and keep track of the largest element found so far var i = nums.length - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; } // If the largest element is not the first element, then swap it with the next largest element if (i >= 0) { var j = nums.length - 1; while (j >= 0 && nums[j] <= nums[i]) { j--; } swap(nums, i, j); } // Reverse the order of the elements from the end of the array reverse(nums, i + 1); return nums; } function swap(nums, i, j) { var temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } function reverse(nums, start) { var i = start, j = nums.length - 1; while (i < j) { swap(nums, i, j); i++; j--; } }
The time complexity is O(n) and the space complexity is O(1).
Here is the code in Java:
public class Solution { public void nextPermutation(int[] nums) { // Start from the second last element in the array and keep track of the largest element found so far int i = nums.length - 2; while (i >= 0 && nums[i] >= nums[i + 1]) { i--; } // If the largest element is not the first element, then swap it with the next largest element if (i >= 0) { int j = nums.length - 1; while (j >= 0 && nums[j] <= nums[i]) { j--; } swap(nums, i, j); } // Reverse the order of the elements from the end of the array reverse(nums, i + 1); } public void swap(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } public void reverse(int[] nums, int start) { int i = start, j = nums.length - 1; while (i < j) { swap(nums, i, j); i++; j--; } } }
Other Array Problems: