【leetcode】801. Minimum Swaps To Make Sequences Increasing
题目如下:
We have two integer sequences
AandBof the same non-zero length.We are allowed to swap elements
A[i]andB[i]. Note that both elements are in the same index position in their respective sequences.At the end of some number of swaps,
AandBare both strictly increasing. (A sequence is strictly increasing if and only ifA[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, Bare 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].
解题思路:每个下标对应的元素只有交换和不交换两种选择,记dp[i][0]为在[0~i]这个区间内,在第i个元素不交换时使得[0~i]区间子数组严格递增时总的交换次数,而dp[i][0]为在[0~i]这个区间内,在第i个元素交换时使得[0~i]区间子数组严格递增时总的交换次数。要使得数组严格递增,第i个元素是否需要交换取决于与(i-1)元素的值的大小情况,总得来说分为可能性如下,
1. A[i] > A[i - 1] and B[i] > B[i - 1] and A[i] > B[i - 1] and B[i] > A[i - 1] ,这种情况下,第i个元素可以交换或者不交换,并且和i-1是否交换没有任何关系,那么可以得出: 在第i个元素不交换的情况下,dp[i][0] 应该等于第i-1个元素交换与不交换两种情况下的较小值,有 dp[i][0] = min(dp[i][0], dp[i - 1][0], dp[i - 1][1]) ,如果第i个元素非要任性的交换,那么结果就是第i-1个元素交换与不交换两种情况下的较小值加上1,有dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1, dp[i - 1][1] + 1) 。
2. A[i] > A[i - 1] and B[i] > B[i - 1] ,这种情况是i和i-1之间要么都交换,要么都不交换。有 dp[i][0] = min(dp[i][0], dp[i - 1][0]) ,dp[i][1] = min(dp[i][1], dp[i - 1][1] + 1)
3. A[i] > B[i - 1] and B[i] > A[i - 1] and (A[i] <= A[i - 1] or B[i] <= B[i - 1]),这种情况是要么i交换,要么i-1交换。有 dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1),dp[i][0] = min(dp[i][0], dp[i - 1][1])
4.其他情况则表示无论i交换或者不交换都无法保证严格递增。
代码如下:
class Solution(object):
def minSwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
dp = [[float('inf')] * 2 for _ in A]
dp[0][0] = 0
dp[0][1] = 1
for i in range(1, len(A)):
if (A[i] > A[i - 1] and B[i] > B[i - 1]) and (A[i] > B[i - 1] and B[i] > A[i - 1]):
dp[i][0] = min(dp[i][0], dp[i - 1][0], dp[i - 1][1])
dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1, dp[i - 1][1] + 1)
elif A[i] > A[i - 1] and B[i] > B[i - 1]:
dp[i][0] = min(dp[i][0], dp[i - 1][0])
dp[i][1] = min(dp[i][1], dp[i - 1][1] + 1)
elif A[i] > B[i - 1] and B[i] > A[i - 1] and (A[i] <= A[i - 1] or B[i] <= B[i - 1]):
dp[i][1] = min(dp[i][1], dp[i - 1][0] + 1)
dp[i][0] = min(dp[i][0], dp[i - 1][1]) #print dp
return min(dp[-1]) if min(dp[-1]) != float('inf') else -1
【leetcode】801. Minimum Swaps To Make Sequences Increasing的更多相关文章
- 【LeetCode】801. Minimum Swaps To Make Sequences Increasing 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 参考资料 日期 题目地址:https:// ...
- LeetCode 801. Minimum Swaps To Make Sequences Increasing
原题链接在这里:https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/ 题目: We have two in ...
- 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 ...
- 【leetcode】1247. Minimum Swaps to Make Strings Equal
题目如下: You are given two strings s1 and s2 of equal length consisting of letters "x" and &q ...
- 【LeetCode】1151. Minimum Swaps to Group All 1's Together 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...
- [LeetCode] 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 ...
- 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 elem ...
- 【leetcode】963. Minimum Area Rectangle II
题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...
- 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)
[LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...
随机推荐
- 【Linux开发】linux设备驱动归纳总结(三):2.字符型设备的操作open、close、read、write
linux设备驱动归纳总结(三):2.字符型设备的操作open.close.read.write 一.文件操作结构体file_operations 继续上次没讲完的问题,文件操作结构体到底是什么东西, ...
- ubuntu服务器上用Nginx和Uwsgi部署django项目
开发环境:ubuntu系统,python3环境 django项目目录: fast_foot 为项目根目录,app为项目应用 现在,我们登陆远程服务器 安装Nginx 安装好了,我们看一下nginx的配 ...
- 小米手机Toast带app名称
如果用小米手机做测试,会发现,Toast弹窗有可能会在前面带app名称.这是因为你传入的context是activity,如果是Application的话,就不会显示app名称.但是,我做测试时,一般 ...
- C# async await的使用
async 声明一个包含异步代码的函数,该函数执行时不会阻塞调用线程. async标记的函数返回值必须为 void ,Task,Task<TResult> await 必须修饰Task 或 ...
- Python assert 关键字
Python assert(断言)用于判断一个表达式,在表达式条件为 False 的时候触发异常. 断言可以在条件不满足程序运行的情况下直接返回错误,而不必等待程序运行后出现崩溃的情况. 语法格式: ...
- PTA(Advanced Level)1011.World Cup Betting
With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excite ...
- kafka整理笔记笔记
一.为什么需要消息系统 解耦: 允许你独立的扩展或修改两边的处理过程,只要确保它们遵守同样的接口约束. 冗余: 消息队列把数据进行持久化直到它们已经被完全处理,通过这一方式规避了数据丢失风险.许多消息 ...
- C++多线程基础学习笔记(一)
下面分三个方面多线程技术的必须掌握一些基本知识. 1.进程 2.线程 3.并发 (1)进程 一个可执行程序运行起来了,即为创建了一个进程.如在电脑上打开了word,就创建了一个word进程,打开QQ, ...
- C++学习 之 类中的特殊函数和this指针(笔记)
1.构造函数 构造函数是一种特殊的函数,它在对象被创建时被调用,与类同名无返回类型,可以被重载.构造函数的可以在类内实现也可以在类外实现. 构造函数的声明类似于下面的代码: class Human { ...
- Android渐变色xml配置
这里渐变色: <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android=&quo ...