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

Approach #1: Brute force. [C++][TEL]

class Solution {
public:
int minSwap(vector<int>& A, vector<int>& B) {
int ans = INT_MAX;
dfs(A, B, 1, 0, ans);
return ans;
} private:
void dfs(vector<int>& A, vector<int>& B, int i, int c, int& ans) {
if (c >= ans) return;
if (i == A.size()) {
ans = min(ans, c);
return;
} if (A[i] > A[i-1] && B[i] > B[i-1])
dfs(A, B, i+1, c, ans); if (A[i] > B[i-1] && B[i] > A[i-1]) {
swap(A[i], B[i]);
dfs(A, B, i+1, c+1, ans);
swap(A[i], B[i]);
}
}
};

  

Approach #2: DP. [Java]

class Solution {
public int minSwap(int[] A, int[] B) {
int n = A.length; int[] keep = new int[n];
int[] swap = new int[n]; Arrays.fill(keep, Integer.MAX_VALUE);
Arrays.fill(swap, Integer.MAX_VALUE); keep[0] = 0;
swap[0] = 1; for (int i = 1; i < n; ++i) {
if (A[i] > A[i-1] && B[i] > B[i-1]) {
keep[i] = keep[i-1];
swap[i] = swap[i-1] + 1;
} if (A[i] > B[i-1] && B[i] > A[i-1]) {
swap[i] = Math.min(swap[i], keep[i-1] + 1);
keep[i] = Math.min(keep[i], swap[i-1]);
}
} return Math.min(keep[n-1], swap[n-1]);
}
}

  

Analysis:

This problem can be solved using dynamic programming, at each position, we can choose to swap or not. Since we want two sorted arrays, at each position, whether to swap or not depends on the choice at previous position, so we can form a recursive formula.

When A[0, i-1] and B[0, i-1] are sorted, since "It is guaranted that the given input always makes it possible.". there are two cases on index i:

They are both still sorted when add number at index i, A[i] > A[i-1] && B[i] > B[i-1]

They are not sorted when add number at index i, in this case, only A[i] > B[i-1] && B[i] > A[i-1] can guarantee that "the given input always makes it possible".

swap[i] to represent the minimum swaps to make the A[0, i] and B[0, i] equences increasing for 0 <= i <= n in condition that we swap A[i] and B[i].

keep[i] torepresent the minimum swaps to make the A[0, i] and B[i] sequences increasing for 0 <= i <= n in condition that we don't swap A[i] and B[i].

Reference:

http://zxi.mytechroad.com/blog/dynamic-programming/leetcode-801-minimum-swaps-to-make-sequences-increasing/

https://leetcode.com/problems/minimum-swaps-to-make-sequences-increasing/discuss/120516/C%2B%2B-solution-with-explanation

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

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

  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. 【LeetCode】801. Minimum Swaps To Make Sequences Increasing 解题报告(Python)

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

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

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

  8. 使序列有序的最少交换次数(minimum swaps)

    交换相邻两数 如果只是交换相邻两数,那么最少交换次数为该序列的逆序数. 交换任意两数 数字的总个数减去循环节的个数?? A cycle is a set of elements, each of wh ...

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

随机推荐

  1. Spring框架的IOC之注解方式的快速入门

    1. 步骤一:导入注解开发所有需要的jar包 * 引入IOC容器必须的6个jar包 * 多引入一个:Spring框架的AOP的jar包,spring-aop的jar包 2. 步骤二:创建对应的包结构, ...

  2. MD5加密获得文件的MD5码

    哈希函数将任意长度的二进制字符串映射为固定长度的小型二进制字符串.加密哈希函数有这样一个属性:在计算不大可能找到散列为相同的值的两个不同的输入:也就是说,两组数据的哈希值仅在对应的数据也匹配时才会匹配 ...

  3. htons、htonl与字节序大小端

    判断字节序大小端code #include <stdio.h> int main() { ) == ) printf("big endian\n"); else pri ...

  4. PS、AI、AE常用快捷键大全

    PS,AI,AE最常用的快捷键来了. 注意:Mac用户请自觉把Ctrl换成Command理解就行. 2017 Adobe Photoshop CC 快捷键 2017Adobe Illustrator快 ...

  5. OSGi 系列(七)之服务的监听、跟踪、声明等

    OSGi 系列(七)之服务的监听.跟踪.声明等 1. OSGi 服务的事件监听 和 bundle 的事件监听类似,服务的事件监听是在服务注册.注销,属性被修改的时候,OSGi 框架会发出各种不同的事件 ...

  6. jquery validate 之多tab页同时校验问题

    1.设置多tab页同时校验: $("form").validate({ignore: ":hidden", ignore: ""}); 由于 ...

  7. 使用dos 作为中介实现cpython 和c# 交互

    最近在使用python 处理一些图形的东西. 实现:对一些512 的图进行像素遍历RGBA 变量, 查询通道不是 255 255 255 颜色 的矩阵,进行切图到空白 之前使用c#进行 确实快10 倍 ...

  8. jquery判断显示的元素并获取显示元素数据

    // 获取显示元素的数据 jQuery(this).find("a:visible").attr("href"); // 多级标签选择器 jQuery(&quo ...

  9. 2018.09.14 洛谷P3567 [POI2014]KUR-Couriers(主席树)

    传送门 简单主席树啊. 但听说有随机算法可以秒掉%%%(本蒟蒻并不会) 直接维护值域内所有数的出现次数之和. 当这个值不大于区间总长度的一半时显然不存在合法的数. 这样在主席树上二分查值就行了. 代码 ...

  10. UVa 11636 Hello World! (水题思维)

    题意:给你一个数,让你求需要复制粘贴多少次才能达到这个数. 析:这真是一个水题,相当水,很容易知道每次都翻倍,只要大于等于给定的数就ok了. 代码如下: #include <iostream&g ...