• 【leetcode】1187. Make Array Strictly Increasing


    题目如下:

    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

    解题思路:如果arr1[i]个元素需要交换的话,那么一定是和arr2中大于arr1[i-1]的所有值中最小的那个交换。在这个前提下,可以利用动态规划的思想来解决这个问题。首先对arr2去重排序,记dp[i][j] = v 表示使得arr1在0~i区间递增需要的最小交换次数为v,并且最后一个交换的操作是 arr1[i] 与 arr2[j]交换,由于存在不需要交换的情况,所以令 dp[i][len(arr2)]为arr1[i]为不需要交换。因为题目要保证递增,所以只需要关注arr1[i-1]与arr[i]的值即可,而两者之间只有以下四种情况:

    1. arr1[i] 与 arr1[i-1]都不交换,这个的前提是 arr1[i]  > arr1[i-1],有 dp[i][len(arr2)] = dp[i-1][len(arr2)] ;

    2. 只有arr1[i] 需要交换,对于任意的arr2[j] > arr1[i-1],都有 dp[i][j] = dp[i-1][len(arr2)] + 1;

    3. 只有arr1[i-1] 需要交换,对于任意的 arr2[j] < arr1[i],都有 dp[i][len(arr2)]  = dp[i-1][j] + 1;

    4.两者都要交换,如果i-1与j-1交换,那么i就和j交换,有dp[i][j] = dp[i-1][j-1] + 1

    最后的结果只需要求出四种情况的最小值即可。

    代码如下:

    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;
        }
    };
  • 相关阅读:
    购物商城实战
    Git(使用码云)
    Git(创建与合并分支)
    Git(工作区和暂存区概念)
    Git(删除文件)
    关于sqoop导入数据的时候添加--split-by配置项对sqoop的导入速度的影响。
    在hue当中设置hive当中数据库的控制权限。
    关于在hue当中执行定时任务,时间的设置。
    关于在hue当中调shell脚本oozie出现017: Could not lookup launched hadoop Job ID [job_1537350421540_0007] which was associated with action [0000003-180919174749982-oozie-oozi-W@shell-9865]. Failing this action!
    关于在在hue当中执行shell脚本使用oozie调度,一直出现laucher异常退出。
  • 原文地址:https://www.cnblogs.com/seyjs/p/11866017.html
Copyright © 2020-2023  润新知