原题链接在这里:https://leetcode.com/problems/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[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].

题解:

Let swap denotes minimum swaps till index i ending with swapping A[i] and B[i].

Let fix denotes minimum swaps till index i ending with NOT swapping A[i] and B[i].

There would be 3 cases:

case1, A[i] <= B[i-1] || B[i] <= A[i-1], i step choice should be same as i-1 step. If i-1 use swap, i need swap. If i-1 use fix, i need fix. Otherwise, i swap would make the result invalid.

case 2, A[i] <= A[i-1] || B[i] <= B[i-1], i step choice should be opposite from i-1 step. If i-1 use swap, i need fix. If i-1 use fix, i need swap. Otherwise, if both swap, the result would be invalid.

case 3, all others, swap or fix are both okay. Then swap takes previous min(swap, fix) plus 1. fix takes previous min(swap, fix) and no change.

Time Complexity: O(A.length).

Space: O(1).

AC Java:

 class Solution {
public int minSwap(int[] A, int[] B) {
if(A == null || B == null || A.length < 2){
return 0;
} int fix = 0;
int swap = 1;
for(int i = 1; i<A.length; i++){
if(A[i] <= B[i-1] || B[i] <= A[i-1]){
swap = swap+1;
}else if(A[i] <= A[i-1] || B[i] <= B[i-1]){
int temp = swap;
swap = fix+1;
fix = temp;
}else{
int min = Math.min(swap, fix);
swap = min + 1;
fix = min;
}
} return Math.min(swap, fix);
}
}

LeetCode 801. Minimum Swaps To Make Sequences Increasing的更多相关文章

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

  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. Java基础教程(26)--反射

    一.类   对于每一种类型的对象,Java虚拟机都会实例化一个java.lang.Class类的不可变实例.该实例提供了获取对象的运行时属性的方法,包括它的成员和类型信息.Class类还提供了创建新实 ...

  2. Django框架(十二)-- 中间件、CSRF跨站请求伪造

    中间件 一.什么是中间件 请求的时候需要先经过中间件才能到达django后端(urls,views,templates,models) 响应的时候也需要经过中间件才能到达web服务网关接口 djang ...

  3. 关于使用K8S的技术流程

    部署Gogs版本管理系统 地址:https://gogs.io/docs 部署Harbor私有仓库 地址:https://github.com/goharbor/harbor/blob/master/ ...

  4. java之mybatis之模糊查询

    1.在 mybatis 中,模糊查询可以有以下方式 (1).第一种,直接将封装好的条件传给 sql 语句 <select id="findByName" parameterT ...

  5. Python DBUtils 连接池对象 (PooledDB)

    数据处理框架用到 mysql, 需要在多进程中的多线程中使用 mysql 的连接 使用到的模块: DBUtils 实现: 使用 DBUtils 中的 PooledDB 类来实现. 自己写一个类, 继承 ...

  6. 珠宝juelrye英语juelrye宝石

    jewellery (usually uncountable, plural jewelleries) 1.(British spelling, Canadian) Collectively, per ...

  7. Dockerfile 基本命令

    1. 前言 Dockerfile 是用来构建自定义 Docker 镜像的文本文档.我们通过docker build 命令用于从Dockerfile 文件构建镜像.如果你要构建自定义镜像,Dockerf ...

  8. 彻底解决unable to find valid certification path to requested target

    安装证书. 下载证书 第一步是要下载证书 去你程序要访问的网站,点击那个锁按钮,并点击查看详情(chrome浏览器) 点击View certificate 点击详细信息 复制到文件 下一步 选择格式 ...

  9. MySQL Replication--半同步复制(Semi-Sync Replication)

    半同步复制 默认配置下,MYSQL主从库通过binlog来保持一致,主库事务提交后,将binlog日志写入磁盘,然后返回给用户,备库通过拉取主库的binlog来同步主库的操作,无法保证主备节点数据实时 ...

  10. idea中flink启动报错org.apache.flink.api.common.ExecutionConfig$GlobalJobParameters

    启动时出现如下报错: Caused by: java.lang.ClassNotFoundException: org.apache.flink.api.common.ExecutionConfig$ ...