题目如下:

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;
}
};

【leetcode】1187. Make Array Strictly Increasing的更多相关文章

  1. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  2. 【LeetCode】659. Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  3. 【leetcode】905. Sort Array By Parity

    题目如下: 解题思路:本题和[leetcode]75. Sort Colors类似,但是没有要求在输入数组本身修改,所以难度降低了.引入一个新的数组,然后遍历输入数组,如果数组元素是是偶数,插入到新数 ...

  4. 【LeetCode】896. Monotonic Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  5. 【LeetCode】673. Number of Longest Increasing Subsequence

    题目: Given an unsorted array of integers, find the number of longest increasing subsequence. Example ...

  6. 【LeetCode】1020. Partition Array Into Three Parts With Equal Sum 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  7. 【LeetCode】457. Circular Array Loop 环形数组是否存在循环 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题思路 快慢指针 代码 日期 题目地址:https://le ...

  8. 【LeetCode】950. Reveal Cards In Increasing Order 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 模拟 日期 题目地址:https://leetcod ...

  9. 【LeetCode】932. Beautiful Array 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 构造法 递归 相似题目 参考资料 日期 题目地址:h ...

随机推荐

  1. shell - python 函数式编程 -- 经典例子 + 让数据自增 + while + > /dev/null 2>&1 & crontab

    1.shell #!/bin/bash anynowtime="date +'%Y-%m-%d %H:%M:%S'" NOW="echo [\`$anynowtime\` ...

  2. [Python3] 041 文件 持久化

    目录 文件 持久化 1. pickle 1.1 例子1 1.2 例子2 1.3 注意 2. shelve 2.1 举例 2.2 特性 2.3 强制写回 2.4 使用 with 管理上下文环境 文件 持 ...

  3. Codeforces 1209D Cow and Snacks

    题目大意 有 $n$ 个不同的糖果,从 $1$ 到 $n$ 编号.有 $k$ 个客人.要用糖果招待客人. 对于每个客人,这些糖果中恰有两个是其最爱.第 $i$ 个客人最爱的糖果编号是 $x_i$ 和 ...

  4. 简单了解运用Git

    Git是 Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的分布式版本控制系统. SVN是集中式的. Git没有中央服务器,每台电脑就是一个完整的版本库 ,工作无需 ...

  5. win10自带虚拟机的使用(Hyper-v)

    昨天刚发现的觉得特别好用,故推荐一下,跟VM虚拟机的使用方法是一样的 1.点击开始菜单中的<设置>图标,进入设置页码 2.点击<应用>图标,进入应用页码,并找到程序和功能 3. ...

  6. spring boot JPA 数据库连接池释放

    当JPA获取数据库数据连接时,如果连接数超过最大连接数的配置,系统就会报错: Unable to acquire JDBC Connection 和: Caused by: java.sql.SQLT ...

  7. sql server if else

    DECLARE IF (@sex = '1')BEGIN PRINT '2'END ELSE BEGIN PRINT(1) END begin... end可以省略 declare @sex int ...

  8. 利用yaml文件管理资源

    利用yaml配置文件管理资源 [root@master ~]# cat nginx-deployment.yaml apiVersion: apps/v1beta2 kind: Deployment ...

  9. openlayers之全屏控件的使用

    import { FullScreen } from 'ol/control' map.addControl(new FullScreen())

  10. 90. Subsets II (Java)

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...