[leetcode] 1503: Previous Permutation With One Swap

Thinking

1. Find a number of exchange A

If the input value of each digit is incremented by the number of bits, the exchange value can not be determined less permutation.

-> turn, that is, if the input from right to left digit is diminishing the value of all there will be no answer .

-> Note If this does not satisfy the conditions on the specification can be replaced, from right to left to find the first violation in descending order of the number of A, need to exchange with A certain number of bits .

    Can be shown: 1) the right side of the array A is descending, the exchange can not be obtained inside a smaller value; numerical weights 2) A greater weight of the left, the left by the number of A bound to talk to a smaller value obtained digital exchange A switching with smaller.

    So in order to get exchange with A is smaller than the original input values, the maximum value of the permutation. (It is smaller and is the largest so hard to express what the hell

2. Find another number exchange B

Another number B must exchange the right of A, the value A is smaller than the number.

Because: 1) If A and left a number of C exchange, to get less than the input of the original value number then C must larger than A, reducing the value of influence in the left digit of the weight must be better than in the right big on digital .

         2) A is the first element of a violation of the right-to-left rule in descending order, A right side there must be less than the value of A number.

And because A is the right in descending order from right to left, B should be wherein A is smaller than the rightmost number. But there will be considered, A is not strictly the right in descending order, there may be adjacent to B and B is equal to the value of the number D, in this case this group should be selected equal to the number of adjacent leftmost. Because the B and D must be younger than A, A, and to exchange a number of which obtained the largest number of (what the hell expression) A small value should make the most left and the number of exchange which, to a large value of A on the weight as high as possible position.

May further estimate B is on the right A, the value A is smaller than the number of right most, if equal thereto adjacent the selected number of most left . (What on earth is really so hard to express

Summary: from right to left to find the first violation of the law in descending order of the number of A, find A value less than the number of A's "most right, equal to the number in the adjacent leftmost" number of B, exchange AB. If you can not find A description is not exchangeable digits.

I really do not prove increasingly refuse the language of mathematics

 

code

 

class Solution {
public:
    vector<int> prevPermOpt1(vector<int>& A) {
        int N=A.size(), left;
        for (left=N-1;left>0;left--) {
            if (A[left-1]>A[left])
                break;
        }
        if (left==0)
            return A;
        --left;
        int right=N-1;
        while (A[right]>=A[left])
            --right;
        while (A[right]==A[right-1])
            --right;
        swap(A[left],A[right]);
        return A;
    }
};

 

Guess you like

Origin www.cnblogs.com/RDaneelOlivaw/p/10958704.html