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.

  1. Example:
  2. Input: A = [1,3,5,4], B = [1,2,3,7]
  3. Output: 1
  4. Explanation:
  5. Swap A[3] and B[3]. Then the sequences are:
  6. A = [1, 3, 5, 7] and B = [1, 2, 3, 4]
  7. 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].

给两个长度相等的数组A和B,可在任意位置i交换A[i]和B[i]的值,使得数组A和B变成严格递增的数组,求最少需要交换的次数。

解法:dp

Java:

  1. class Solution {
  2. public int minSwap(int[] A, int[] B) {
  3. int swapRecord = 1, fixRecord = 0;
  4. for (int i = 1; i < A.length; i++) {
  5. if (A[i - 1] >= B[i] || B[i - 1] >= A[i]) {
  6. // In this case, the ith manipulation should be same as the i-1th manipulation
  7. // fixRecord = fixRecord;
  8. swapRecord++;
  9. } else if (A[i - 1] >= A[i] || B[i - 1] >= B[i]) {
  10. // In this case, the ith manipulation should be the opposite of the i-1th manipulation
  11. int temp = swapRecord;
  12. swapRecord = fixRecord + 1;
  13. fixRecord = temp;
  14. } else {
  15. // Either swap or fix is OK. Let's keep the minimum one
  16. int min = Math.min(swapRecord, fixRecord);
  17. swapRecord = min + 1;
  18. fixRecord = min;
  19. }
  20. }
  21. return Math.min(swapRecord, fixRecord);
  22. }
  23. }  

Python:

  1. class Solution(object):
  2. def minSwap(self, A, B):
  3. """
  4. :type A: List[int]
  5. :type B: List[int]
  6. :rtype: int
  7. """
  8. dp_no_swap, dp_swap = [0]*2, [1]*2
  9. for i in xrange(1, len(A)):
  10. dp_no_swap[i%2], dp_swap[i%2] = float("inf"), float("inf")
  11. if A[i-1] < A[i] and B[i-1] < B[i]:
  12. dp_no_swap[i%2] = min(dp_no_swap[i%2], dp_no_swap[(i-1)%2])
  13. dp_swap[i%2] = min(dp_swap[i%2], dp_swap[(i-1)%2]+1)
  14. if A[i-1] < B[i] and B[i-1] < A[i]:
  15. dp_no_swap[i%2] = min(dp_no_swap[i%2], dp_swap[(i-1)%2])
  16. dp_swap[i%2] = min(dp_swap[i%2], dp_no_swap[(i-1)%2]+1)
  17. return min(dp_no_swap[(len(A)-1)%2], dp_swap[(len(A)-1)%2])  

C++:

  1. class Solution {
  2. public:
  3. int minSwap(vector<int>& A, vector<int>& B) {
  4. int n = A.size();
  5. vector<int> swap(n, n), noSwap(n, n);
  6. swap[0] = 1; noSwap[0] = 0;
  7. for (int i = 1; i < n; ++i) {
  8. if (A[i] > A[i - 1] && B[i] > B[i - 1]) {
  9. swap[i] = swap[i - 1] + 1;
  10. noSwap[i] = noSwap[i - 1];
  11. }
  12. if (A[i] > B[i - 1] && B[i] > A[i - 1]) {
  13. swap[i] = min(swap[i], noSwap[i - 1] + 1);
  14. noSwap[i] = min(noSwap[i], swap[i - 1]);
  15. }
  16. }
  17. return min(swap[n - 1], noSwap[n - 1]);
  18. }
  19. };

C++:

  1. class Solution {
  2. public:
  3. int minSwap(vector<int>& A, vector<int>& B) {
  4. int n1 = 0, s1 = 1, n = A.size();
  5. for (int i = 1; i < n; ++i) {
  6. int n2 = INT_MAX, s2 = INT_MAX;
  7. if (A[i - 1] < A[i] && B[i - 1] < B[i]) {
  8. n2 = min(n2, n1);
  9. s2 = min(s2, s1 + 1);
  10. }
  11. if (A[i - 1] < B[i] && B[i - 1] < A[i]) {
  12. n2 = min(n2, s1);
  13. s2 = min(s2, n1 + 1);
  14. }
  15. n1 = n2;
  16. s1 = s2;
  17. }
  18. return min(n1, s1);
  19. }
  20. };

  

类似题目:

Best Time to Buy and Sell Stock with Transaction Fee  

All LeetCode Questions List 题目汇总

[LeetCode] 801. Minimum Swaps To Make Sequences Increasing 最少交换使得序列递增的更多相关文章

  1. LeetCode 801. Minimum Swaps To Make Sequences Increasing

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

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

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

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

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

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

  6. [LeetCode] 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. [Swift]LeetCode801. 使序列递增的最小交换次数 | 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 ...

  8. [LeetCode] 727. Minimum Window Subsequence 最小窗口序列

    Given strings S and T, find the minimum (contiguous) substring W of S, so that T is a subsequence of ...

  9. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

随机推荐

  1. KM(Kuhn-Munkres)算法求带权二分图的最佳匹配

    KM(Kuhn-Munkres)算法求带权二分图的最佳匹配 相关概念 这个算法个人觉得一开始时有点难以理解它的一些概念,特别是新定义出来的,因为不知道是干嘛用的.但是,在了解了算法的执行过程和原理后, ...

  2. centos7部署inotify与rsync实现实时数据同步

    实验环境:CentOS Linux release 7.6.1810 node1:192.168.216.130 客户端(向服务端发起数据同步) node2:192.168.216.132 服务端(接 ...

  3. 机房断电,导致xfs文件系统损坏

    记一次机房断电,导致xfs文件系统损坏处理方法 挂载时报以下错误: mount: mount /dev/sdb on /dev/sdb failed: Structure needs cleaning ...

  4. B-树,B+树,B*树总结

    链接地址:https://blog.csdn.net/v_JULY_v/article/details/6530142 B+树 B+ 树是一种树数据结构,是一个n叉树,每个节点通常有多个孩子,一棵B+ ...

  5. 如何使用keil5将stm32的hal库编译成lib文件——F1版本

    hal库中keil5中编译的速度是比较慢的,相同情况下,每次都要编译的时候,比标准库是要慢很多的,因此就hal库编译成lib文件是一种加快编译速度的方法,当然也有其自身的缺点.一.步骤1.使用cube ...

  6. tensorflow API _ 4 (优化器配置)

    """Configures the optimizer used for training. Args: learning_rate: A scalar or `Tens ...

  7. 关于原生js的节点兼容性

    关于节点的兼容性: 1:获取元素的子节点 a: childNodes:获取元素的子节点,空文本,非空文本,注释,获取的比较全面, 如果只是想获取元素的子节点,请用(children) b:     c ...

  8. 使用SpringBoot访问jsp页面

    1 编写application.yml文件 spring: mvc: view: suffix: .jsp prefix: /jsp/ 2 创建Controller层 @Controller @Req ...

  9. SpringCloud组件Eureka

    什么是微服务架构 架构设计概念,各服务间隔离(分布式也是隔离),自治(分布式依赖整体组合)其它特性(单一职责,边界,异步通信,独立部署)是分布式概念的跟严格执行SOA到微服务架构的演进过程作用:各服务 ...

  10. S1_搭建分布式OpenStack集群_05 glance安装配置

    一.基本简介         镜像服务(glance)使用户能够发现,注册和检索虚拟机镜像. 它提供了一个REST API,使您可以查询虚拟机镜像元数据并检索实际镜像. 您可以将通过镜像服务提供的虚拟 ...