zoukankan      html  css  js  c++  java
  • 801. Minimum Swaps To Make Sequences Increasing

    We have two integer sequences A and B of the same non-zero length.

    We are allowed to swap elements A[i] and B[i].  Note that both elements are in the same index position in their respective sequences.

    At the end of some number of swaps, A and B are both strictly increasing.  (A sequence is strictly increasing if and only if A[0] < A[1] < A[2] < ... < A[A.length - 1].)

    Given A and B, return the minimum number of swaps to make both sequences strictly increasing.  It is guaranteed that the given input always makes it possible.

    Example:
    Input: A = [1,3,5,4], B = [1,2,3,7]
    Output: 1
    Explanation: 
    Swap A[3] and B[3].  Then the sequences are:
    A = [1, 3, 5, 7] and B = [1, 2, 3, 4]
    which are both strictly increasing.

    Note:

    • A, B are arrays with the same length, and that length will be in the range [1, 1000].
    • A[i], B[i] are integer values in the range [0, 2000].

    Approach #1: Brute force. [C++][TEL]

    class Solution {
    public:
        int minSwap(vector<int>& A, vector<int>& B) {
            int ans = INT_MAX;
            dfs(A, B, 1, 0, ans);
            return ans;
        }
        
    private:
        void dfs(vector<int>& A, vector<int>& B, int i, int c, int& ans) {
            if (c >= ans) return;
            if (i == A.size()) {
                ans = min(ans, c);
                return;
            }
            
            if (A[i] > A[i-1] && B[i] > B[i-1]) 
                dfs(A, B, i+1, c, ans);
            
            if (A[i] > B[i-1] && B[i] > A[i-1]) {
                swap(A[i], B[i]);
                dfs(A, B, i+1, c+1, ans);
                swap(A[i], B[i]);
            }
        }
    };
    

      

    Approach #2: DP. [Java]

    class Solution {
        public int minSwap(int[] A, int[] B) {
            int n = A.length;
            
            int[] keep = new int[n];
            int[] swap = new int[n];
            
            Arrays.fill(keep, Integer.MAX_VALUE);
            Arrays.fill(swap, Integer.MAX_VALUE);
            
            keep[0] = 0;
            swap[0] = 1;
            
            for (int i = 1; i < n; ++i) {
                if (A[i] > A[i-1] && B[i] > B[i-1]) {
                    keep[i] = keep[i-1];
                    swap[i] = swap[i-1] + 1;
                }
                
                if (A[i] > B[i-1] && B[i] > A[i-1]) {
                    swap[i] = Math.min(swap[i], keep[i-1] + 1);
                    keep[i] = Math.min(keep[i], swap[i-1]);
                }
            }
            
            return Math.min(keep[n-1], swap[n-1]);
        }
    }
    

      

    Analysis:

    This problem can be solved using dynamic programming, at each position, we can choose to swap or not. Since we want two sorted arrays, at each position, whether to swap or not depends on the choice at previous position, so we can form a recursive formula.

    When A[0, i-1] and B[0, i-1] are sorted, since "It is guaranted that the given input always makes it possible.". there are two cases on index i:

    They are both still sorted when add number at index i, A[i] > A[i-1] && B[i] > B[i-1] 

    They are not sorted when add number at index i, in this case, only A[i] > B[i-1] && B[i] > A[i-1] can guarantee that "the given input always makes it possible".

    swap[i] to represent the minimum swaps to make the A[0, i] and B[0, i] equences increasing for 0 <= i <= n in condition that we swap A[i] and B[i].

    keep[i] torepresent the minimum swaps to make the A[0, i] and B[i] sequences increasing for 0 <= i <= n in condition that we don't swap A[i] and B[i].

    Reference:

    http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-801-minimum-swaps-to-make-sequences-increasing/

    https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/discuss/120516/C%2B%2B-solution-with-explanation

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    URAL 1207 极角排序
    URAL 1348 求垂足
    POJ 3608 旋转卡壳
    POJ 2954 Pick定理
    POJ 2007 叉积排序
    UVA 10002 凸多边形重心
    [转]bad interpreter:No such file or directory的原因
    [转]实现Python HTTP服务方法大总结
    urllib2.HTTPError: HTTP Error 403: Forbidden 错误
    C++二分查找代码
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10561659.html
Copyright © 2011-2022 走看看