31. Next Permutation

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, do not allocate 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,31,3,2
3,2,11,2,3

1,1,51,5,1


官方解答如下:

Summary

We need to find the next lexicographic permutation of the given list of numbers than the number formed by the given array.

Solution


Approach #1 Brute Force [Time Limit Exceeded]

Algorithm

In this approach, we find out every possible permutation of list formed by the elements of the given array and find out the permutation which isjust larger than the given one. But this one will be a very naive approach, since it requires us to find out every possible permutation which will take really long time and the implementation is complex. Thus, this approach is not acceptable at all. Hence, we move on directly to the correct approach.

Complexity Analysis

  • Time complexity : O(n!)O(n!)O(n!). Total possible permutations is n!n!n!.
  • Space complexity : O(n)O(n)O(n). Since an array will be used to store the permutations.

Approach #2 Single Pass Approach [Accepted]

Algorithm

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]a[i]a[i] and a[i−1]a[i-1]a[i1], from the right, which satisfy a[i]>a[i−1]a[i] > a[i-1]a[i]>a[i1]. Now, no rearrangements to the right of a[i−1]a[i-1]a[i1] 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]a[i-1]a[i1] 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]a[i-1]a[i1] with the number which is just larger than itself among the numbers lying to its right section, say a[j]a[j]a[j].

 Next Permutation

We swap the numbers a[i−1]a[i-1]a[i1] and a[j]a[j]a[j]. We now have the correct number at index i−1i-1i1. 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]a[i-1]a[i1]. 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]a[i]a[i] and a[i−1]a[i-1]a[i1] where, a[i]>a[i−1]a[i] > a[i-1]a[i]>a[i1]. Thus, all numbers to the right of a[i−1]a[i-1]a[i1] were already sorted in descending order. Furthermore, swapping a[i−1]a[i-1]a[i1] and a[j]a[j]a[j] didn't change that order. Therefore, we simply need to reverse the numbers following a[i−1]a[i-1]a[i1] to get the next smallest lexicographic permutation.

The following animation will make things clearer:

Next Permutation

Java

public class Solution {
    public void nextPermutation(int[] nums) {
        int i = nums.length - 2;
        while (i >= 0 && nums[i + 1] <= nums[i]) {
            i--;
        }
        if (i >= 0) {
            int j = nums.length - 1;
            while (j >= 0 && nums[j] <= nums[i]) {
                j--;
            }
            swap(nums, i, j);
        }
        reverse(nums, i + 1);
    }

    private void reverse(int[] nums, int start) {
        int i = start, j = nums.length - 1;
        while (i < j) {
            swap(nums, i, j);
            i++;
            j--;
        }
    }

    private void swap(int[] nums, int i, int j) {
        int temp = nums[i];
        nums[i] = nums[j];
        nums[j] = temp;
    }
}

Complexity Analysis

  • Time complexity : O(n)O(n)O(n). In worst case, only two scans of the whole array are needed.

  • Space complexity : O(1)O(1)O(1). No extra space is used. In place replacements are done.


Analysis written by: @vinod23


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值