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 ...
随机推荐
- Python之路【第十五篇】开发FTP多线程程序
要求: 1.用户加密认证 2.允许同时多用户登录 3.每个用户有自己的家目录,且只能访问自己的家目录 4.对用户进行磁盘配额,每个用户的可用空间不同 5.允许用户在ftp server上随意切换目录 ...
- vps建站施工预告
作为一个小白,最近几天自己用vps搭了个站点,用来发发博客,偶尔还可以去外面看看.后面几章就来记一下过程吧! 结构极为简单,建站用的WordPress,目前也就只有最基础的发文章功能.不过由于习惯了m ...
- sublime配置python环境及快捷键
sublime配置python环境 参考链接:https://blog.csdn.net/VertigozZ/article/details/54574006 快捷键的配置:https://www.c ...
- 【翻译】REST framework JWT Auth(django rest framework-jwt)
JWT认证的REST框架 原文链接 概述 这个包提供对Django REST framework的JSON Web Token 认证支持. 需要满足条件 Python (2.7, 3.3, 3.4, ...
- 高并发场景下System.currentTimeMillis()的性能问题的优化
高并发场景下System.currentTimeMillis()的性能问题的优化 package cn.ucaner.alpaca.common.util.key; import java.sql.T ...
- .net core 读取、修改配置文件appsettings.json
.net core 设置读取JSON配置文件 appsettings.json Startup.cs 中 public class Startup { public Startup(IHostingE ...
- .NET-异步操作
感觉可以用于log日志的东西,这个东西他还是会走的但是不会影响你下一步的操作,你下一步还是正常怎么操作就怎么操作! 这样可以给用户免掉一些没必要的等待. static void Main(string ...
- python中通过selenium简单操作及xpath元素定位&轴定位
浏览器的简单操作 # 导入webdriver模块 # 创建driver对象,指定Chrome浏览器 driver = webdriver.Chrome() # 窗口最大化 driver.maximiz ...
- mysql-表关系介绍(应用较多)
目录 表之间的关系(重点) foreign key (外键) 级联操作 (cascade) 两种级联操作 外键的使用 多对一(一对多) 多对多 一对一关系 表之间的关系(重点) foreign key ...
- 换个语言学一下 Golang (9)——结构体和接口
基本上到这里的时候,就是上了一个台阶了.Go的精华特点即将展开. 结构体定义 上面我们说过Go的指针和C的不同,结构体也是一样的.Go是一门删繁就简的语言,一切令人困惑的特性都必须去掉. 简单来讲,G ...