ans
, s0
, and s1
to 1, representing the length of the longest non-decreasing subarray found so far, and the lengths of the subarrays ending at the previous index for nums1
and nums2
, respectively.n - 1
.i
, update s0
and s1
as follows:
s0
becomes the maximum of s0
(if nums1[i-1] <= nums1[i]
) and s1
(if nums2[i-1] <= nums1[i]
) plus 1.s1
becomes the maximum of s0
(if nums1[i-1] <= nums2[i]
) and s1
(if nums2[i-1] <= nums2[i]
) plus 1.ans
with the maximum of ans
, s0
, and s1
.ans
as the result.