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, 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的更多相关文章
- [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 ...
随机推荐
- Go基础编程实践(八)—— 系统编程
捕捉信号 // 运行此程序,控制台将打印"Waiting for signal" // 按Ctrl + C 发送信号以关闭程序,将发生中断 // 随后控制台依次打印"Si ...
- 【LEETCODE】70、字符匹配1023 Camelcase Matching
最近做leetcode总感觉自己是个智障,基本很少有题能自己独立做出来,都是百度... 不过终于还是做出了一题...而且速度效率还可以 哎,加油吧,尽量锤炼自己 package y2019.Algor ...
- Drools7 Hello Wrold 入门详细步骤--系列01课
一.什么叫规则引擎?规则--->写在文档上引擎--->在java代码上,引用这个文档上的规则 二.drools规则引擎有什么用?简单来说就是将多变的规则,从业务代码中剥离出来(当规则变了之 ...
- Java File类 mkdir 不能创建多层目录
File f = new File("/home/jp/Upload"); if ((!f.exists()) || (!f.isDirectory())) {boolean re ...
- OO第三单元作业总结
OO第三单元作业总结--JML 第三单元的主题是JML规格的学习,其中的三次作业也是围绕JML规格的实现所展开的(虽然感觉作业中最难的还是如何正确适用数据结构以及如何正确地对于时间复杂度进行优化). ...
- 十大Intellij IDEA快捷键(附IDEA快捷键详细列表及使用技巧)
十大Intellij IDEA快捷键(附IDEA快捷键详细列表及使用技巧) Intellij IDEA中有很多快捷键让人爱不释手,stackoverflow上也有一些有趣的讨论.每个人都有自己的最爱, ...
- python 工厂方法
工厂方法模式(FACTORY METHOD)是一种常用创建型设计模式,此模式的核心精神是封装类中变化的部分,提取其中个性化善变的部分为独立类, 通过依赖注入以达到解耦.复用和方便后期维护拓展的目的. ...
- python之路第一天
2018-07-11星期三 创建自己的博客(博客园): 登陆 我的博客 随笔:所有人在博客中都能看见的文章 文章:别人看不见,只能URL访问--我把网页地址发给你,你才能看到 日志:别人看不到,URL ...
- requests中构造post请求注意点
构造post请求时需要注意点: 通过requests.post()进行POST请求时,传入报文的参数有两个,一个是data,一个是json. 如果是urlencoded 格式 data=字典如果是js ...
- React: webpack模块组织关系
现代前端开发离不开打包工具,以 webpack 为代表的打包工具已经成为日常开发必备之利器,拿 React 技术栈为例,我们 ES6 形式的源代码,需要经过 webpack 和 Babel 处理,才能 ...