zoukankan      html  css  js  c++  java
  • Next Permutation

    Problem Statement

    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,3 → 1,3,2

    3,2,1 → 1,2,3

    1,1,5 → 1,5,1

     

    The hard point of this problem is the definition of next permutation. The description of how to generate next permutation is an algorithm to solve this problem.

    From wikipedia, there's a simple algorithm. But we need to give some remarks to analyze it.

    1. Find the largest index $k$ such that $a[k] < a[k + 1]$. If no such index exists, the permutation is the last permutation.
    2. Find the largest index $l$ greater than $k$ such that $a[k] < a[l]$.
    3. Swap the value of $a[k]$ with that of $a[l]$.
    4. Reverse the sequence from $a[k + 1]$ up to and including the final element $a[n]$.

    The reason why we search from right to left, because that's the first place to decrease the value of permutation.

     

    If we get to the beginning, that means what we deal is the minimum permutation.

     

    Otherwise, if we get the largest $k$ such that $a[k] < a[k+1]$, that means:

    • $a[k+1]$ to $a[n]$ is in decreasing order
    • $a[k]$ is also one element in the range from $a[n]$ to $a[k]$.

    These mean we've got the last permutation when we fix {$a[1], a[2], ..., a[k]$}.

    So the next permutation should begin with {$a[1], a[2], ..., a[k-1], a[k']$}, where $a[k']$ is the next larger element than $a[k]$. After that, we start our permutation from $$a[1], a[2], ..., a[k-1], a[k'], a[k+2], ..., a[n]$$, where $$a[1], a[2], ..., a[k-1], a[k']$$ is in increasing order.

    So, in order to get the next permutation, first we need to find the next larger element than $a[k]$. The method we use is step 2:

    Find the largest index $l$ greater than $k$ such that $a[k] < a[l]$.

    Then, we need to swap the two elements $a[k]$ and $a[l]$. Now:

    1. the next permutation's begining part {$a[1], a[2], ..., a[k]$} has been reached right places.
    2. the new $a[l]$ is just less than $a[l-1]$ and greater than $a[l+1]$. So, {$a[k+1], ..., a[n]$} is in decreasing order.

    So, the last step, we need to reverse the remaining parts, {$a[k+1], ..., a[n]$}, to get the right next permutation.

     


    The complete code is: 

    class Solution {
        void swap(vector<int> &num, int i , int j){
            if (i == j) return;
            
            int tmp = num[i];
            num[i] = num[j];
            num[j] = tmp;
        }
        
        void reverse(vector<int> &num, int start, int end){
            if(start >= end) return;
            
            while(start < end){
                swap(num, start, end);
                start++;
                end--;
            }
        }
        
    public:
        void nextPermutation(vector<int> &num) {
            int n = (int)num.size();
            
            if(1 == n) return;
            
            int p = 0, l = 0;
            for(int i = n - 1; i > 0; --i){
                if(num[i] > num[i-1]){
                    p = i - 1;
                    l = i;
                    
                    while(num[l] > num[p] && l < n) ++l;
                    
                    --l;
                    swap(num, p, l);
                    reverse(num, p+1, n-1);
                    return;
                }
            }
            
            reverse(num, 0, n-1);
        }
    };
    
  • 相关阅读:
    json schema相关
    好看的记录片和电影
    java函数局部变量的坑(非常隐藏)
    maven操作
    Js数组的常用的方法概述
    深入理解JS各种this指向问题
    浅谈ES5和ES6继承和区别
    vue
    使用 vue-i18n 切换中英文
    js_数组对象的浅克隆
  • 原文地址:https://www.cnblogs.com/kid551/p/4113273.html
Copyright © 2011-2022 走看看