Longest Selectable Non-Decreasing Subarray
You are given two integer arrays A and B of equal length.
For any contiguous subarray of indices, you may choose exactly one value from either A[i] or B[i] at each index i in that subarray. Your goal is to make the chosen values form a non-decreasing sequence.
Return the maximum possible length of such a contiguous subarray.
Complete the function longestSelectableNonDecreasingSubarray in the editor below.
longestSelectableNonDecreasingSubarray has the following parameters:
int[] A: the first arrayint[] B: the second array
Returns
int: the maximum valid contiguous length.
1Example 1
Choose the sequence [1, 2, 5, 7] by taking A[0], B[1], A[2], and B[3]. It is non-decreasing, so the full length 4 is valid.
2Example 2
Choose [1, 2, 3] from array B. The entire array is a valid non-decreasing contiguous subarray.
Constraints
Limits and guarantees your solution can rely on.
The source thread did not provide explicit numeric bounds.
A.length == B.length- You must choose exactly one of
A[i]orB[i]for each index in the selected subarray.