forked from kamyu104/LeetCode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstructing-two-increasing-arrays.cpp
More file actions
36 lines (34 loc) · 1.04 KB
/
constructing-two-increasing-arrays.cpp
File metadata and controls
36 lines (34 loc) · 1.04 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
// Time: O(m * n)
// Space: O(min(m, n))
// dp
class Solution {
public:
int minLargest(vector<int>& nums1, vector<int>& nums2) {
static const int INF = numeric_limits<int>::max();
if (size(nums1) < size(nums2)) {
swap(nums1, nums2);
}
vector<int> dp(size(nums2) + 1, INF);
dp[0] = 0;
for (int i = 0; i <= size(nums1); ++i) {
for (int j = 0; j <= size(nums2); ++j) {
if (!i && !j) {
continue;
}
int curr = INF;
if (i - 1 >= 0) {
if (dp[j] != INF) {
curr = min(curr, dp[j] + (dp[j] % 2 == nums1[i - 1] % 2 ? 2 : 1));
}
}
if (j - 1 >= 0) {
if (dp[j - 1] != INF) {
curr = min(curr, dp[j - 1] + (dp[j - 1] % 2 == nums2[j - 1] % 2 ? 2 : 1));
}
}
dp[j] = curr;
}
}
return dp.back();
}
};