-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path1187.java
More file actions
65 lines (65 loc) · 2.11 KB
/
Copy path1187.java
File metadata and controls
65 lines (65 loc) · 2.11 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// 1187. Make Array Strictly Increasing
//
// Given two integer arrays arr1 and arr2, return the minimum number of operations (possibly zero) needed to make arr1 strictly increasing.
//
// In one operation, you can choose two indices 0 <= i < arr1.length and 0 <= j < arr2.length and do the assignment arr1[i] = arr2[j].
//
// If there is no way to make arr1 strictly increasing, return -1.
//
//
//
// Example 1:
//
// Input: arr1 = [1,5,3,6,7], arr2 = [1,3,2,4]
// Output: 1
// Explanation: Replace 5 with 2, then arr1 = [1, 2, 3, 6, 7].
// Example 2:
//
// Input: arr1 = [1,5,3,6,7], arr2 = [4,3,1]
// Output: 2
// Explanation: Replace 5 with 3 and then replace 3 with 4. arr1 = [1, 3, 4, 6, 7].
// Example 3:
//
// Input: arr1 = [1,5,3,6,7], arr2 = [1,6,3,3]
// Output: -1
// Explanation: You can't make arr1 strictly increasing.
//
//
// Constraints:
//
// 1 <= arr1.length, arr2.length <= 2000
// 0 <= arr1[i], arr2[i] <= 10^9
//
// Runtime 25ms Beats 97.84%of users with Java
// Memory 62.19MB Beats 72.43%of users with Java
class Solution {
public int makeArrayIncreasing(int[] arr1, int[] arr2) {
TreeSet<Integer> set = new TreeSet<>();
for (int num: arr2) {
set.add(num);
}
int len = arr1.length;
int steps = Math.min(set.size(), len);
int[][] matrix = new int[len + 1][steps + 1];
for (int i = 0; i <= len; i++) {
Arrays.fill(matrix[i], Integer.MAX_VALUE);
}
matrix[0][0] = -1;
for (int i = 1; i <= len; i++) {
for (int j = 0; j <= steps; j++) {
if (i < j) continue;
if (arr1[i - 1] > matrix[i - 1][j]) {
matrix[i][j] = arr1[i - 1];
}
if (j > 0 && matrix[i - 1][j - 1] != Integer.MAX_VALUE) {
Integer replacement = set.higher(matrix[i - 1][j - 1]);
if (replacement != null) {
matrix[i][j] = Math.min(matrix[i][j], replacement);
}
}
if (i == len && matrix[i][j] != Integer.MAX_VALUE) return j;
}
}
return -1;
}
}