【leetcode】1187. Make Array Strictly Increasing

Topics are as follows:

Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.

In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].

If there is no way to make arr1 strictly increasing, return -1.

Example 1:

Input: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
Output: 1
Explanation: Replace 5 with 2, then arr1 = [1, 2, 3, 6, 7].

Example 2:

Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
Output: 2
Explanation: Replace 5 with 3 and then replace 3 with 4. arr1 = [1, 3, 4, 6, 7].

Example 3:

Input: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
Output: -1
Explanation: You can't make arr1 strictly increasing.

Constraints:

  • 1 <= arr1.length, arr2.length <= 2000
  • 0 <= arr1[i], arr2[i] <= 10^9

Outline of Solution: If arr1 [i] elements to be exchanged, then it must be greater than all values and arr2 arr1 [i-1] of the smallest of the exchange. In this context, we can use dynamic programming ideas to solve this problem. First arr2 to reorder, referred dp [i] [j] = v represents such arr1 the minimum number of exchanges 0 ~ i interval increment required is v, and the operation for the final exchange is arr1 [I] and arr2 [J] exchange, without the need for the presence of the exchange, so that it dp [i] [len (arr2 )] to arr1 [i] is unnecessary exchange. Because the topics to ensure incremental, so only we need to focus on arr1 [i-1] and arr [i] value to, and only four cases between the two:

1. arr1 [i] and arr1 [i-1] is not exchanged, the premise is arr1 [i]> arr1 [i-1], there dp [i] [len (arr2)] = dp [i-1 ] [len (arr2)];

2. Only arr1 [i] needs to be exchanged, for any arr2 [j]> arr1 [i-1], there dp [i] [j] = dp [i-1] [len (arr2)] + 1;

3. Only arr1 [i-1] need to be exchanged, for any arr2 [j] <arr1 [i], there dp [i] [len (arr2)] = dp [i-1] [j] + 1;

4. Both should be exchanged, if i-1 and j-1 exchange, then i and j on the exchange, there dp [i] [j] = dp [i-1] [j-1] + 1

The end result can only requires a minimum of four cases.

code show as below:

class Solution {
public:
    int makeArrayIncreasing(vector<int>& arr1, vector<int>& arr2) {
        set<int> st(arr2.begin(), arr2.end());
        //arr2.clear();
        arr2.assign(st.begin(), st.end());
        //arr2.sort();
        sort(arr2.begin(), arr2.end());
        vector <vector<int>> dp ;for (int i =0;i< arr1.size();i++){
            vector<int> v2 (arr2.size()+1,2001);
            dp.push_back(v2);
        }
        for (int i = 0 ;i < arr2.size();i++){
            dp[0][i] = 1;
        }
        int res = 2001;
        int LAST_INDEX = arr2.size();
        dp[0][arr2.size()] = 0;
        //int ] = 0;
        for (int i =1 ;i < arr1.size();i++){
            for (int j = 0;j < arr2.size();j++){
                //only [i] exchange
                if (arr2[j] > arr1[i-1]){
                    dp[i][j] = min(dp[i][j],dp[i-1][LAST_INDEX] + 1);
                }
                //both [i] and [i-1] exchange
                if(j > 0){
                    dp[i][j] = min(dp[i][j],dp[i-1][j-1] + 1);
                }
                //only [i-1] change
                if (arr1[i] > arr2[j]){
                    dp[i][LAST_INDEX] = min(dp[i][LAST_INDEX],dp[i-1][j]);
                }
            }
            // no exchange
            if (arr1[i] > arr1[i-1]){
                dp[i][LAST_INDEX] = min(dp[i][LAST_INDEX],dp[i-1][LAST_INDEX]);
            }
        }
        for (int i = 0; i <= arr2.size();i++){
            res = min(res,dp[arr1.size()-1][i]);
        }
        return res == 2001 ? -1 : res;
    }
};

 

Guess you like

Origin www.cnblogs.com/seyjs/p/11866017.html