/**
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
Leetcode ref:https://evelynn.gitbooks.io/google-interview/next_permutation.html
*/
/**
Sol:https://leetcode.com/problems/next-permutation/solution/
First, we observe that for any given sequence that is in descending order, no next larger permutation is possible. For example, no next permutation is possible for the following array:
[9, 5, 4, 3, 1]
We need to find the first pair of two successive numbers a[i] and a[i−1], from the right, which satisfy a[i] > a[i-1]. Now, no rearrangements to the right of a[i−1] can create a larger permutation since that subarray consists of numbers in descending order. Thus, we need to rearrange the numbers to the right of a[i-1] including itself.
Now, what kind of rearrangement will produce the next larger number? We want to create the permutation just larger than the current one. Therefore, we need to replace the number a[i−1] with the number which is just larger than itself among the numbers lying to its right section, say a[j].
We swap the numbers a[i-1] and a[j]. We now have the correct number at index i−1. But still the current permutation isn't the permutation that we are looking for. We need the smallest permutation that can be formed by using the numbers only to the right of a[i−1]. Therefore, we need to place those numbers in ascending order to get their smallest permutation.
But, recall that while scanning the numbers from the right, we simply kept decrementing the index until we found the pair a[i] and a[i−1] where, a[i] > a[i-1]. Thus, all numbers to the right of a[i−1] were already sorted in descending order. Furthermore, swapping a[i−1] and a[j] didn't change that order. Therefore, we simply need to reverse the numbers following a[i−1] to get the next smallest lexicographic permutation.
*/
import java.util.Arrays;
public class NextPermutation{
public static void nextPermutation(int[] nums) {
// find two adjacent elements, n[i-1] < n[i]
int i = nums.length - 1;
for (; i > 0; i --) {
if (nums[i] > nums[i-1]) {
break;
}
}
if (i != 0) {//if we did not reach the begining of the array (the case when the array is already in descending order)
// swap (i-1, j), where j is index of the smallest number in [i, n)
int j = nums.length - 1;
for (; j >= i; j --) {
if (nums[j] > nums[i-1]) {
break;
}
}
swap(nums, i - 1, j);
}
reverse(nums, i);
}
public static void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public static void reverse(int[] nums, int start){
int end = nums.length-1;
while(start<end){
swap(nums, start, end);
start++;
end--;
}
}
public static void main(String[] args){
int[] nums = new int[]{1,2,4,3};
nextPermutation(nums);
System.out.println(Arrays.toString(nums));
}
}
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
Leetcode ref:https://evelynn.gitbooks.io/google-interview/next_permutation.html
*/
/**
Sol:https://leetcode.com/problems/next-permutation/solution/
First, we observe that for any given sequence that is in descending order, no next larger permutation is possible. For example, no next permutation is possible for the following array:
[9, 5, 4, 3, 1]
We need to find the first pair of two successive numbers a[i] and a[i−1], from the right, which satisfy a[i] > a[i-1]. Now, no rearrangements to the right of a[i−1] can create a larger permutation since that subarray consists of numbers in descending order. Thus, we need to rearrange the numbers to the right of a[i-1] including itself.
Now, what kind of rearrangement will produce the next larger number? We want to create the permutation just larger than the current one. Therefore, we need to replace the number a[i−1] with the number which is just larger than itself among the numbers lying to its right section, say a[j].
We swap the numbers a[i-1] and a[j]. We now have the correct number at index i−1. But still the current permutation isn't the permutation that we are looking for. We need the smallest permutation that can be formed by using the numbers only to the right of a[i−1]. Therefore, we need to place those numbers in ascending order to get their smallest permutation.
But, recall that while scanning the numbers from the right, we simply kept decrementing the index until we found the pair a[i] and a[i−1] where, a[i] > a[i-1]. Thus, all numbers to the right of a[i−1] were already sorted in descending order. Furthermore, swapping a[i−1] and a[j] didn't change that order. Therefore, we simply need to reverse the numbers following a[i−1] to get the next smallest lexicographic permutation.
*/
import java.util.Arrays;
public class NextPermutation{
public static void nextPermutation(int[] nums) {
// find two adjacent elements, n[i-1] < n[i]
int i = nums.length - 1;
for (; i > 0; i --) {
if (nums[i] > nums[i-1]) {
break;
}
}
if (i != 0) {//if we did not reach the begining of the array (the case when the array is already in descending order)
// swap (i-1, j), where j is index of the smallest number in [i, n)
int j = nums.length - 1;
for (; j >= i; j --) {
if (nums[j] > nums[i-1]) {
break;
}
}
swap(nums, i - 1, j);
}
reverse(nums, i);
}
public static void swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
public static void reverse(int[] nums, int start){
int end = nums.length-1;
while(start<end){
swap(nums, start, end);
start++;
end--;
}
}
public static void main(String[] args){
int[] nums = new int[]{1,2,4,3};
nextPermutation(nums);
System.out.println(Arrays.toString(nums));
}
}
No comments:
Post a Comment