-
Notifications
You must be signed in to change notification settings - Fork 35
/
minimumswapstomakesequencesincreasing.java
36 lines (29 loc) · 1.23 KB
/
minimumswapstomakesequencesincreasing.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
class Solution {
public int minSwap(int[] nums1, int[] nums2) {
int n = nums1.length;
// initialize dp table
int[][] memo = new int[2][n];
Arrays.fill(memo[0], -1);
Arrays.fill(memo[1], -1);
memo[0][0] = 0;
memo[1][0] = 1;
return Math.min(recurse(nums1, nums2, n - 1, 0, memo),
recurse(nums1, nums2, n - 1, 1, memo));
}
private int recurse(int[] nums1, int[] nums2, int i, int swap, int[][] memo) {
//check dp table
if (memo[swap][i] != -1)
return memo[swap][i];
// initial value is set as max
int res = Integer.MAX_VALUE;
// if array is increasing without swapping
if (nums1[i] > nums1[i - 1] && nums2[i] > nums2[i - 1])
res = recurse(nums1, nums2, i - 1, swap, memo);
// if array is increasing with swapping
if (nums1[i] > nums2[i - 1] && nums2[i] > nums1[i - 1])
res = Math.min(res,
recurse(nums1, nums2, i - 1, 1 - swap, memo));
memo[swap][i] = swap == 0 ? res : res + 1;
return memo[swap][i];
}
}