LeetCode 801. Minimum Swaps To Make Sequences Increasing
原题链接在这里: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, Bare 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的更多相关文章
- [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 ...
- 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 ...
- 【LeetCode】801. Minimum Swaps To Make Sequences Increasing 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 参考资料 日期 题目地址:https:// ...
- 【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 ...
- 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 ...
- [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 ...
- [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 ...
- [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 ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
随机推荐
- 超级简单POI导出Excel实战
在一般的生产管理系统都会将数据通过页面导出到Excel,这里以Java为例通过第三方开源poi进行对Excel的操作,具体操作如下 1.引入jar包依赖 这里我以maven的方式引入jar包,具体依赖 ...
- 长乐国庆集训Day2
T1 连珠风暴 题目 [题目描述] 给定M种颜色的珠子,每种颜色珠子的个数均不限,将这些珠子做成长度为N的项链. 问能做成多少种不重复的项链.两条项链相同,当且仅当两条项链通过旋转或是翻转后能重合在一 ...
- Linux 线程间的同步与互斥
在线程并发执行的时候,我们需要保证临界资源的安全访问,防止线程争抢资源,造成数据二义性. 线程同步: 条件变量 为什么使用条件变量? 对临界资源的时序可控性,条件满足会通知其他等待操作临界资源的线程, ...
- centos 7安装jdk并封装service服务
一.概述 有一个Spring Cloud的jar包,文件名为:RDS.jar.必须要jdk1.8版本,需要部署在 Centos 7.5的服务器上面,最好能设置开机自启动! 二.安装jdk 关闭防火墙 ...
- 软件测试人员必备的Python知识图库
UI自动化测试(Python+Selenium等) 接口测试(Python requests等) 性能测试(Python Locust等) 安全性测试(Python Scapy等) 兼容性测试(Pyt ...
- CentOS7安装MySQL(完整版)
在CentOS中默认安装有MariaDB,这个是MySQL的分支,但为了需要,还是要在系统中安装MySQL,而且安装完成之后可以直接覆盖掉MariaDB. 1 下载并安装MySQL官方的 Yum Re ...
- ChineseNumber 转换
中文数字转换 /** * <html> * <body> * <P> Copyright 1994 JsonInternational</p> * &l ...
- DbParameter关于Like查询的传参数无效问题
用传参方式模糊查询searchName 按常规的思路,我们会这样写 ,代码如下: String searchName ="Sam"; String strSql = "s ...
- Filter DSL 常用语法 -- 基本查询语法,必会
转发自:https://www.cnblogs.com/ghj1976/p/5293250.html term 过滤 term主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed ...
- Matlab装饰模式
装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构.根据https://www.runoob.com/design-pattern/decorator ...