Longest Non-decreasing Subarray From Two Arrays

You are given two 0-indexed integer arrays nums1 and nums2 of length n.

Define another 0-indexed integer array, nums3, of length n. For each index i in the range [0, n - 1], you can assign either nums1[i] or nums2[i] to nums3[i].

Your task is to maximize the length of the longest non-decreasing subarray in nums3 by choosing its values optimally.

Return an integer representing the length of the longest non-decreasing subarray in nums3.

Note: A subarray is a contiguous non-empty sequence of elements within an array.

Example 1
Inputnums1 = [2,3,1], nums2 = [1,2,1]
Output2
Choosing nums3 = [2, 2, 1] gives a non-decreasing subarray [2, 2] of length 2, which is the maximum achievable length.
Example 2
Inputnums1 = [1,3,2,1], nums2 = [2,2,3,4]
Output4
Choosing nums3 = [1, 2, 3, 4] makes the entire array a non-decreasing subarray of length 4.

Constraints

  • 1 <= nums1.length == nums2.length == n <= 10^5
  • 1 <= nums1[i], nums2[i] <= 10^9

Asked at 5 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate