Incrementing minimize exchange sequence

We have equal lengths two integer not empty array A and B.

We can exchange elements A [i] and B [i] is. Note that each of these two elements in the sequence should be in the same position.

After an exchange of some elements of the array A and B should be strictly increasing (incrementing stringent conditions only array A [0] <A [1] <A [2] <... <A [A.length - 1] ).

Given array A and B, so that the return to the two arrays are kept strictly increasing the minimum number of switching states. Assuming that a given input always effective.

Example:

Input: A = [1,3,5,4], B = [1,2,3,7]
Output: 1
Explanation: 
After the exchange of A [3] and B [3], the following two arrays:
A = [1, 3, 5, 7] , B = [1, 2, 3, 4]
The two arrays are strictly increasing.

note:

A, B the length of the two arrays are always equal, and the length of the range [1, 1000].
A [i], B [i] are [0, 2000] integer in the interval.

Best way points Analysis : This question is a bit more difficult for the analysis of induction capacity requirements pricey.
Just start it, may we all greedy thinking about using a similar strategy, is experiencing A [i - 1]> = A [i] This reverse order, we will be A [i] and B [i] exchange, then it will be a test example stumped

0   4   4   5
0   1   6   8

First met A [i - 1] when> = A [i], i = 2, in accordance with the greedy strategy, we should exchange A [2] and B [2], we found that after swapping A [3] appears the reverse order, then there is a need to exchange a [3] and B [3], so that a total of two exchanges. This question needs to be exchanged only egg type 1, which we exchange A [1] and B [1] can be achieved while A, B strictly increasing. Therefore, this simple greedy strategy does not work.

A search of realization of others, found a way to dynamic programming.

The minimum number of exchanges swapVec [i] denotes the i-th switching elements, so that A [0, i], B [0, i] is strictly monotonic increasing
keepVec [i] denotes the i-th element is not exchanged, so that A [0, i], B [0, i] strictly monotonic increasing the minimum number of exchange.

The question now is how do we find the state transition equation.

(1) When A [i]> A [i - 1] && B [i]> B [i - 1], in this case, the increment itself, this need not be exchanged A [i] and B [ i]
		swapVec [i] indicates a mandatory exchange A [i], B [i] that is the i-th switching element, then the first i-1 have a position exchange, the exchange can continue while at the same time guarantee increment, so swapVec [i] = swapVec [ i - 1] +1
		keepVec [i] represents the exchange not A [i], B [i], i.e., without exchanging the i-th element of the i-1 th switching element is not required, keepVec [i] = keepVec [i - 1]
(2) if A [i]> B [i - 1] && B [i]> A [i - 1], for this situation, it is necessary to exchange A [i], B [i] is incremented in order to maintain
		swapVec [i] is just switched to the current position, a position can not be exchanged and the front, then swapVec [i] = keepVec [i - 1] +1
		keepVec [i] is the current position can not be exchanged, then we can achieve the same location is incremented by one before the exchange, i.e. keepVec [i] = swapVec [i - 1]
Whichever is lower if two conditions are true.

 

class Solution {
public:
    int minSwap(vector<int>& A, vector<int>& B) {
        int numsSize = A.size();
        The minimum number of exchanges // swapVec [i] denotes the i-th switching elements, so that A [0, i], B [0, i] is strictly monotonic increasing
    	// keepVec [i] denotes the i-th element is not exchanged, so that A [0, i], B [0, i] minimum switching frequency of the strictly monotonic increasing.
        vector<int> swapVec(numsSize, INT_MAX), keepVec(numsSize, INT_MAX);
        swapVec [0] = 1; // initialize
        keepVec[0] = 0;
        for (int index = 1; index < numsSize; ++index){
            if (A[index] > A[index - 1] && B[index] > B[index - 1]){
                // If the i-th switching element, then the first i - 1 element must be exchanged; if the i-th element is not switched, then the first i - 1 must not exchange element
                swapVec[index] = swapVec[index - 1] + 1;
                keepVec[index] = keepVec[index - 1];
            }
            if (A[index] > B[index - 1] && B[index] > A[index - 1]){
                // If the i-th switching element, then the first i - 1 element must not be exchanged; if the i-th element is not switched, then the first i - 1 element must be exchanged
                swapVec[index] = min(swapVec[index], keepVec[index - 1] + 1);
                keepVec[index] = min(keepVec[index], swapVec[index - 1]);
            }
        }
        return min(swapVec.back(), keepVec.back());
    }
};

 

Reprinted: https://blog.csdn.net/qq_41855420/article/details/90264842

 

Guess you like

Origin www.cnblogs.com/wuchanming/p/12237298.html