-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path1187.cpp
30 lines (28 loc) · 961 Bytes
/
1187.cpp
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
class Solution {
public:
int makeArrayIncreasing(vector<int>& arr1, vector<int>& arr2) {
sort(arr2.begin(), arr2.end());
int n = arr1.size();
arr1.insert(arr1.begin(), 0);
vector<vector<int>> dp(n + 1, vector<int>(n + 1, INT_MAX));
dp[0][0] = INT_MIN;
for (int i = 1; i <= n; ++i) {
for (int j = 0; j <= i; ++j) {
if (dp[i - 1][j] < arr1[i]) {
dp[i][j] = min(dp[i][j], arr1[i]);
}
if (j >= 1) {
auto it = upper_bound(arr2.begin(), arr2.end(), dp[i - 1][j - 1]);
if (it != arr2.end()) {
dp[i][j] = min(dp[i][j], *it);
}
}
}
}
int res = INT_MAX;
for (int j = 0; j <= n; ++j) {
if (dp[n][j] != INT_MAX) res = min(res, j);
}
return res == INT_MAX ? -1 : res;
}
};