Longest Selectable Non-Decreasing Subarray
Learn this problemProblem statement
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.
Function
longestSelectableNonDecreasingSubarray(A: int[], B: int[]) → intComplete 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.
Examples
Example 1
A = [1, 3, 5, 4]B = [2, 2, 6, 7]return = 4Choose 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.
Example 2
A = [5, 4, 3]B = [1, 2, 3]return = 3Choose [1, 2, 3] from array B. The entire array is a valid non-decreasing contiguous subarray.
Constraints
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.