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. mysql数据库总结。

    mysql MySQL语法MySQL采用结构化查询语言SQL (Structured Query Language)语言来操作数据库SQL语句必须以 ; 结束SQL语句分类DDL(数据定义语言): c ...

  2. Linux TTY介绍

    1. TTY介绍 TTY(TeleType)指Linux中的一类终端(Terminal)设备, 是一种字符设备 在Linux中, tty可分为如下几类- 串行端口终端(serial port term ...

  3. Django REST framework 使用简记

    最近在参与的项目中需要使用到dajngo REST framework工具包进行开发,之前参与的项目几乎都是清一色的使用原生的django(话说偶尔也会使用一下Flask,真心不怎么喜欢这个框架),之 ...

  4. shell脚本自动化安装pgsql10.5版本

    看到有个大佬写了个很实用的脚本,于是这里做了转载 #!/bin/bash #进入软件的制定安装目录 echo "进入目录/usr/local,下载pgsql文件" cd /usr/ ...

  5. crc计算工具推荐

    比较好的crc计算工具,32位64位系统都可以用的.crc的校验方法也很多.推荐使用 下载地址

  6. makefile的三个变量

    “$@”代表目标文件. “$^”代表所有的依赖文件. “$<”代表第一个依赖文件. main:main.o mytool1.o mytool2.o gcc -o $@ $^ .c.o: gcc ...

  7. IGC(Interleaved Group Convolutions)

    深度学习被引起关注是在2012年,用神经网络训练的一个分类模型在ImagNet上取得了第一名,而且其分类精度比第二名高出10多个点,当时所使用的模型为AlexNet,现在看来其为一个比较简单的网络,而 ...

  8. 让一个项目同时提交到码云和GitHub两个仓库

    在项目目录里找到.git文件夹然后找到config文件. 打开这个文件后找到下面的代码 [remote "origin"] url = git提交地址 fetch = +refs/ ...

  9. 001_Visual Studio 显示数组波形

    视频教程:https://v.qq.com/x/page/z3039pr02eh.html 资料下载:https://download.csdn.net/download/xiaoguoge11/12 ...

  10. mybatis-generator 插件

    首先肯定要有mybatis的依赖 <!--mybatis spring--> <dependency> <groupId>org.mybatis.spring.bo ...