Skip to content

Commit 2cbaf55

Browse files
committed
https://leetcode.cn/problems/minimum-swaps-to-make-sequences-increasing/
1 parent 8ee5585 commit 2cbaf55

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/minimum-swaps-to-make-sequences-increasing/
14+
1315
https://leetcode.cn/problems/6eUYwP/
1416

1517
https://leetcode.cn/problems/three-in-one-lcci/
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
export default function minSwap(nums1: number[], nums2: number[]): number {
2+
const n = nums1.length;
3+
let a = 0,
4+
b = 1;
5+
for (let i = 1; i < n; i++) {
6+
const at = a,
7+
bt = b;
8+
a = b = n;
9+
if (nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1]) {
10+
a = Math.min(a, at);
11+
b = Math.min(b, bt + 1);
12+
}
13+
if (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1]) {
14+
a = Math.min(a, bt);
15+
b = Math.min(b, at + 1);
16+
}
17+
}
18+
return Math.min(a, b);
19+
}

0 commit comments

Comments
 (0)