[LeetCode] Constructing Two Increasing Arrays

3269. Constructing Two Increasing Arrays

Given 2 integer arrays nums1 and nums2``nums1 and nums2, after doing the following.

Replace every 0 with an even positive integer and every 1 with an odd positive integer. After replacement, both arrays should be increasing and each integer should be used at most once.

Return the minimum possible largest number after applying the changes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Solution {
public:
int minLargest(vector<int>& A, vector<int>& B) {
int n = A.size(), m = B.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, (n + 1) * (m + 1) * 2));
dp[0][0] = 0;
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= m; j++) {
if(i < n) {
dp[i+1][j] = min(dp[i+1][j], dp[i][j] + ((A[i] ^ dp[i][j]) & 1 ? 1 : 2));
}
if(j < m) {
dp[i][j+1] = min(dp[i][j+1], dp[i][j] + ((B[j] ^ dp[i][j]) & 1 ? 1 : 2));
}
}
}
return dp[n][m];
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2024/08/31/PS/LeetCode/constructing-two-increasing-arrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.