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

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

解法:dp

Java:

class Solution {
public int minSwap(int[] A, int[] B) {
int swapRecord = 1, fixRecord = 0;
for (int i = 1; i < A.length; i++) {
if (A[i - 1] >= B[i] || B[i - 1] >= A[i]) {
// In this case, the ith manipulation should be same as the i-1th manipulation
// fixRecord = fixRecord;
swapRecord++;
} else if (A[i - 1] >= A[i] || B[i - 1] >= B[i]) {
// In this case, the ith manipulation should be the opposite of the i-1th manipulation
int temp = swapRecord;
swapRecord = fixRecord + 1;
fixRecord = temp;
} else {
// Either swap or fix is OK. Let's keep the minimum one
int min = Math.min(swapRecord, fixRecord);
swapRecord = min + 1;
fixRecord = min;
}
}
return Math.min(swapRecord, fixRecord);
}
}  

Python:

class Solution(object):
def minSwap(self, A, B):
"""
:type A: List[int]
:type B: List[int]
:rtype: int
"""
dp_no_swap, dp_swap = [0]*2, [1]*2
for i in xrange(1, len(A)):
dp_no_swap[i%2], dp_swap[i%2] = float("inf"), float("inf")
if A[i-1] < A[i] and B[i-1] < B[i]:
dp_no_swap[i%2] = min(dp_no_swap[i%2], dp_no_swap[(i-1)%2])
dp_swap[i%2] = min(dp_swap[i%2], dp_swap[(i-1)%2]+1)
if A[i-1] < B[i] and B[i-1] < A[i]:
dp_no_swap[i%2] = min(dp_no_swap[i%2], dp_swap[(i-1)%2])
dp_swap[i%2] = min(dp_swap[i%2], dp_no_swap[(i-1)%2]+1)
return min(dp_no_swap[(len(A)-1)%2], dp_swap[(len(A)-1)%2])  

C++:

class Solution {
public:
int minSwap(vector<int>& A, vector<int>& B) {
int n = A.size();
vector<int> swap(n, n), noSwap(n, n);
swap[0] = 1; noSwap[0] = 0;
for (int i = 1; i < n; ++i) {
if (A[i] > A[i - 1] && B[i] > B[i - 1]) {
swap[i] = swap[i - 1] + 1;
noSwap[i] = noSwap[i - 1];
}
if (A[i] > B[i - 1] && B[i] > A[i - 1]) {
swap[i] = min(swap[i], noSwap[i - 1] + 1);
noSwap[i] = min(noSwap[i], swap[i - 1]);
}
}
return min(swap[n - 1], noSwap[n - 1]);
}
};

C++:

class Solution {
public:
int minSwap(vector<int>& A, vector<int>& B) {
int n1 = 0, s1 = 1, n = A.size();
for (int i = 1; i < n; ++i) {
int n2 = INT_MAX, s2 = INT_MAX;
if (A[i - 1] < A[i] && B[i - 1] < B[i]) {
n2 = min(n2, n1);
s2 = min(s2, s1 + 1);
}
if (A[i - 1] < B[i] && B[i - 1] < A[i]) {
n2 = min(n2, s1);
s2 = min(s2, n1 + 1);
}
n1 = n2;
s1 = s2;
}
return min(n1, s1);
}
};

  

类似题目:

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. drf框架(2)

    drf框架 """接口: 接口规范: drf的生命周期: 序列化组件: 三大认证:过滤,删选,排序组件 请求,响应,解析,异常 jwt:json web tooken & ...

  2. qt事件机制(转)

    学习了一段时间的Qt之后,发现Qt的事件机制和其他语言的机制有些不同.Qt除了能够通过信号和槽机制来实现一些Action动作之外,还可以用对象所带的事件,或者用户自定义的事件来实现对象的一些行为处理. ...

  3. keras中to_categorical()函数解析

    from keras.utils.np_utils import * # 类别向量定义 b = [0, 1, 2, 3, 4, 5, 6, 7, 8] # 调用to_categorical将b按照9个 ...

  4. [Luogu 2386]放苹果

    Description 题库链接 把 \(n\) 个同样的苹果放在 \(m\) 个同样的盘子里,允许有的盘子空着不放,问共有多少种不同的分发.多测,数据组数 \(t\). \(1\leq m,n\le ...

  5. 学习:MessageBox API

    函数功能: MessageBox 函数用于显示一个模态对话框,其中包含一个系统图标. 一组按钮和一个简短的特定于应用程序消息,如状态或错误的信息. 消息框中返回一个整数值,该值指示用户单击了哪个按钮. ...

  6. SpringBoot学习(五)RSocket和Security

    一.RSocket RSocket是一个用于字节流传输的二进制协议.它通过在单个连接上传递异步消息来支持对称交互模型,主要支持的通讯层包括 TCP, WebSockets和Aeron(UDP). RS ...

  7. solidworks 学习 (四)

    旋钮三维建模

  8. linux 出错 “INFO: task java: xxx blocked for more than 120 seconds.” 的3种解决方案

    1 问题描述 最近搭建的一个linux最小系统在运行到241秒时在控制台自动打印如下图信息,并且以后每隔120秒打印一次. 仔细阅读打印信息发现关键信息是“hung_task_timeout_secs ...

  9. Why We Changed YugaByte DB Licensing to 100% Open Source

    转自:https://blog.yugabyte.com/why-we-changed-yugabyte-db-licensing-to-100-open-source/ 主要说明了YugaByte ...

  10. Lightning Web Components 组合(五)

    使用组合我们可以用来设计复杂的组件. 组合一些比较小的组件,可以增加组件的重新性以及可维护性. 通过以下一个简单的demo,将会展示关于owner 以及container 的概念,在实际的项目中 ex ...