-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution801.java
More file actions
22 lines (22 loc) · 862 Bytes
/
Copy pathSolution801.java
File metadata and controls
22 lines (22 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution801 {
public int minSwap(int[] nums1, int[] nums2) {
// d[i] 表示使得nums1[0:i]和nums2[0:i]严格递增的最少交换次数
int[] d = new int[nums1.length];
int[] p = new int[nums1.length];
// 边界
d[0] = 0; p[0] = 1;
// d
for (int i = 1; i < d.length; i++) {
d[i] = Integer.MAX_VALUE; p[i] = Integer.MAX_VALUE;
if (nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1]) {
d[i] = Math.min(d[i], d[i - 1]);
p[i] = Math.min(p[i], p[i - 1] + 1);
}
if (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1]) {
d[i] = Math.min(d[i], p[i - 1]);
p[i] = Math.min(p[i], d[i - 1] + 1);
}
}
return Math.min(d[nums1.length - 1], p[nums1.length - 1]);
}
}