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 ...
随机推荐
- 【EBS】XLA_GLT表的清理
一.Xla_glt*在出现在日记账导入中的阶段 与R11使用gl_interface表不同,R12中大部分情况下使用的是XLA_GLT_<groupId>表:子帐传送到总账的过程中,会动态 ...
- 【Oracle】获取SQL执行计划
一.plsql developer工具F5 在sqldeveloper中选中sql按F5即可查看执行计划
- Kafka 消费者
应用从Kafka中读取数据需要使用KafkaConsumer订阅主题,然后接收这些主题的消息.在我们深入这些API之前,先来看下几个比较重要的概念. Kafka消费者相关的概念 消费者与消费组 假设这 ...
- C# vb .net实现淡出效果特效滤镜
在.net中,如何简单快捷地实现Photoshop滤镜组中的淡出效果特效呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第 ...
- SessionChange
protected override void OnSessionChange(SessionChangeDescription changeDescription) { System.IO.File ...
- P2801 教主的魔法 (线段树)
题目 P2801 教主的魔法 解析 成天做水题 线段树,第一问区间加很简单 第二问可以维护一个区间最大值和一个区间最小值,若C小于等于区间最小值,就加上区间长度,若C大于区间最大值,就加0 ps:求教 ...
- MySQL-By孙胜利-sifangku.com
一.数据库基本概念 数据库:信息存储的仓库,包括一系列的关系措施! 表:一个数据库中可以有若干张表(形式上你可以看出我们日常生活中建立的表) 字段:表里面的信息会分若干个栏目来存,这些栏目呢,我们在数 ...
- 《区块链DAPP开发入门、代码实现、场景应用》笔记3——Ethereum Wallet的安装
以太坊官方网站可以下载最新版本的Ethereum Wallet,用户无需选择,浏览器会根据访问者操作系统版本自动展现合适的版本,点击DOWNLOAD按钮下载即可安装,如图2.9所示,其下载网址: ht ...
- 供应链管理如何提高效率?APS系统成优化引擎
APS系统,虽然它的起兴只有短短的十几年,但是在这段时间里面,它为很多企业解决了很多人工手动.脑力不可解决的问题. 所以APS被誉为供应链优化引擎,APS常常被称为高级计划与排程,但也有称为高级计划系 ...
- oracle之PLSQL导出-导入-表-存储过程等操作--亲测好用
1.背景 实际开发中考虑到安全,不会将生产库的数据和本地开发数据进行同步操作,而是采用导入导出sql语句的方式操作; 例如在开发环境写好的存储过程要更新到生产环境,那么就需要使用导出和导入功能. 2. ...