-
Notifications
You must be signed in to change notification settings - Fork 61
Expand file tree
/
Copy path3269-constructing-two-increasing-arrays.js
More file actions
48 lines (43 loc) · 1.44 KB
/
3269-constructing-two-increasing-arrays.js
File metadata and controls
48 lines (43 loc) · 1.44 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
/**
* 3269. Constructing Two Increasing Arrays
* https://leetcode.com/problems/constructing-two-increasing-arrays/
* Difficulty: Hard
*
* Given 2 integer arrays nums1 and nums2 consisting only of 0 and 1, your task is to calculate
* the minimum possible largest number in arrays 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.
*/
/**
* @param {number[]} nums1
* @param {number[]} nums2
* @return {number}
*/
var minLargest = function(nums1, nums2) {
const n1 = nums1.length;
const n2 = nums2.length;
const dp = new Array(n1 + 1).fill().map(() => new Array(n2 + 1).fill(-Infinity));
dp[0][0] = 0;
for (let i = 0; i < n1; i++) {
dp[i + 1][0] = getNextValue(dp[i][0], nums1[i]);
}
for (let j = 0; j < n2; j++) {
dp[0][j + 1] = getNextValue(dp[0][j], nums2[j]);
}
for (let i = 0; i < n1; i++) {
for (let j = 0; j < n2; j++) {
dp[i + 1][j + 1] = Math.min(
getNextValue(dp[i][j + 1], nums1[i]),
getNextValue(dp[i + 1][j], nums2[j])
);
}
}
return dp[n1][n2];
function getNextValue(currentValue, targetParity) {
const nextValue = currentValue + 1;
return nextValue % 2 === targetParity % 2 ? nextValue : nextValue + 1;
}
};