题目如下:

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].

解题思路:每个下标对应的元素只有交换和不交换两种选择,记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的更多相关文章

  1. 【LeetCode】801. Minimum Swaps To Make Sequences Increasing 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 参考资料 日期 题目地址:https:// ...

  2. LeetCode 801. Minimum Swaps To Make Sequences Increasing

    原题链接在这里:https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/ 题目: We have two in ...

  3. 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 ...

  4. 【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 ...

  5. 【LeetCode】1151. Minimum Swaps to Group All 1's Together 解题报告 (C++)

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

  6. [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 ...

  7. 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 ...

  8. 【leetcode】963. Minimum Area Rectangle II

    题目如下: Given a set of points in the xy-plane, determine the minimum area of any rectangle formed from ...

  9. 【LeetCode】452. Minimum Number of Arrows to Burst Balloons 解题报告(Python)

    [LeetCode]452. Minimum Number of Arrows to Burst Balloons 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https ...

随机推荐

  1. 【VS开发】字符,字节和编码

    字符,字节和编码 [原创文章,转载请保留或注明出处:http://www.regexlab.com/zh/encoding.htm] 级别:中级 摘要:本文介绍了字符与编码的发展过程,相关概念的正确理 ...

  2. Spring MVC (二)注解式开发使用详解

    MVC注解式开发即处理器基于注解的类开发, 对于每一个定义的处理器, 无需在xml中注册. 只需在代码中通过对类与方法的注解, 即可完成注册. 定义处理器 @Controller: 当前类为处理器 @ ...

  3. 6.maven的安装

    JAVA配置 JAVA_HOME=安装目录 PATH=%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin CLASSPATH=%JAVA_HOME%\lib\dt.jar;%JAV ...

  4. .Net Core Grpc Consul 实现服务注册 服务发现 负载均衡

    本文是基于..net core grpc consul 实现服务注册 服务发现 负载均衡(二)的,很多内容是直接复制过来的,..net core grpc consul 实现服务注册 服务发现 负载均 ...

  5. HDU-5238 Calculator

    题目描述 给定一个关于 \(x\) 的表达式,形如下例:\(×4+2^3+8×6\) 按如下方法计算:\((((x×4)+2)^3+8)×6\) 运算符只有 加号,乘号,幂运算三种,给定的式子中有 \ ...

  6. jQuery俄罗斯方块游戏动画

    在线演示       本地下载

  7. Android开发build出现java.lang.NumberFormatException: For input string: "tle 0x7f0800aa"错误的解决方案

    查看异常栈没有发现项目代码的问题,因为问题是出现在layout文件中. 全局查找tle这个,发现在某个layout文件中title一词被变成ti tle了,结果Android就xjb报错了. 参考

  8. 一头扎进 JAVA

    硅不可 吉米 JAVA 基础 -- 基础不牢,地动山摇 子类应该比 父类更为 开放 (public protected default private) 子类方法不能比父类抛出更高异常( 可以为父类方 ...

  9. Freemarker生成word文档的时的一些&,>,<报错

    替换模板ftl中的内容的时候,一些特殊的字符需要转移,例如: &,<,> value为字符串 value.replace("&","& ...

  10. Docker容器入门之一:部署SpringBoot项目

    一.环境准备:    1.vm虚拟机: Workstation 12 Pro 12.5.7 build-5813279 2.Centos 7 在虚拟机上安装好Centos7系统后,就可以开始准备安装D ...