Algorithms
Medium
2771
2869
Longest Non-decreasing Subarray From Two Arrays
longest-non-decreasing-subarray-from-two-arrays
"""
You are given two 0-indexed integer arrays nums1 and nums2 of length n.

Let's 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:

Input: nums1 = [2,3,1], nums2 = [1,2,1]
Output: 2
Explanation: One way to construct nums3 is:
nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1].
The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2.
We can show that 2 is the maximum achievable length.

Example 2:

Input: nums1 = [1,3,2,1], nums2 = [2,2,3,4]
Output: 4
Explanation: One way to construct nums3 is:
nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4].
The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.

Example 3:

Input: nums1 = [1,1], nums2 = [2,2]
Output: 2
Explanation: One way to construct nums3 is:
nums3 = [nums1[0], nums1[1]] => [1,1].
The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.


Constraints:

 * 1 <= nums1.length == nums2.length == n <= 105
 * 1 <= nums1[i], nums2[i] <= 109
"""
class Solution:
    def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int:
        
You are given two 0-indexed integer arrays nums1 and nums2 of length n.

Let's 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:

Input: nums1 = [2,3,1], nums2 = [1,2,1]
Output: 2
Explanation: One way to construct nums3 is:
nums3 = [nums1[0], nums2[1], nums2[2]] => [2,2,1].
The subarray starting from index 0 and ending at index 1, [2,2], forms a non-decreasing subarray of length 2.
We can show that 2 is the maximum achievable length.

Example 2:

Input: nums1 = [1,3,2,1], nums2 = [2,2,3,4]
Output: 4
Explanation: One way to construct nums3 is:
nums3 = [nums1[0], nums2[1], nums2[2], nums2[3]] => [1,2,3,4].
The entire array forms a non-decreasing subarray of length 4, making it the maximum achievable length.

Example 3:

Input: nums1 = [1,1], nums2 = [2,2]
Output: 2
Explanation: One way to construct nums3 is:
nums3 = [nums1[0], nums1[1]] => [1,1].
The entire array forms a non-decreasing subarray of length 2, making it the maximum achievable length.


Constraints:

 * 1 <= nums1.length == nums2.length == n <= 105
 * 1 <= nums1[i], nums2[i] <= 109


Please complete the code below to solve above prblem:
```python
class Solution:
    def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int:
        
```

my_solution = Solution()

test_input = { "nums1": [2,3,1], "nums2": [1,2,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [1,3,2,1], "nums2": [2,2,3,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 4

test_input = { "nums1": [1,1], "nums2": [2,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [1], "nums2": [1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [1], "nums2": [2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [1,4], "nums2": [4,19] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [1,8], "nums2": [10,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [1,11], "nums2": [9,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [1,13], "nums2": [18,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [1,19], "nums2": [12,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [1,19], "nums2": [18,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [2,20], "nums2": [1,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [3,5], "nums2": [13,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [3,6], "nums2": [10,12] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [3,7], "nums2": [8,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [3,8], "nums2": [15,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [3,9], "nums2": [11,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [3,12], "nums2": [7,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [3,12], "nums2": [20,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [3,20], "nums2": [5,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [4,2], "nums2": [10,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [4,12], "nums2": [6,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [4,15], "nums2": [3,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [5,5], "nums2": [19,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [5,7], "nums2": [19,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [5,11], "nums2": [2,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [5,14], "nums2": [8,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [5,15], "nums2": [16,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [5,20], "nums2": [4,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [5,20], "nums2": [10,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [6,7], "nums2": [3,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [6,14], "nums2": [5,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [6,14], "nums2": [18,6] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [6,16], "nums2": [16,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [6,17], "nums2": [4,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [7,3], "nums2": [16,7] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [7,4], "nums2": [15,7] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [7,9], "nums2": [3,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [7,10], "nums2": [10,14] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [7,11], "nums2": [5,7] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [7,12], "nums2": [20,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [7,20], "nums2": [12,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [8,5], "nums2": [13,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [8,11], "nums2": [9,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [8,16], "nums2": [9,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [8,17], "nums2": [2,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [8,18], "nums2": [16,12] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [4,2], "nums2": [10,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [9,1], "nums2": [11,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [9,6], "nums2": [8,14] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [9,9], "nums2": [11,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [9,12], "nums2": [20,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [9,15], "nums2": [20,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [9,16], "nums2": [11,15] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [9,19], "nums2": [17,9] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [10,19], "nums2": [17,10] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [11,1], "nums2": [3,11] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [11,3], "nums2": [9,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [11,6], "nums2": [9,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [11,19], "nums2": [17,11] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [11,69], "nums2": [26,62] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [12,1], "nums2": [10,12] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [12,10], "nums2": [16,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [13,6], "nums2": [20,13] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [13,16], "nums2": [5,13] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [14,2], "nums2": [2,14] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [14,4], "nums2": [2,13] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [15,10], "nums2": [17,15] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [15,11], "nums2": [19,2] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [16,9], "nums2": [5,16] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [16,17], "nums2": [9,16] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [17,8], "nums2": [11,10] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [17,10], "nums2": [9,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [17,10], "nums2": [18,7] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [17,11], "nums2": [19,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [17,14], "nums2": [17,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [17,17], "nums2": [15,17] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [17,17], "nums2": [16,1] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [18,4], "nums2": [1,6] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [18,9], "nums2": [10,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [18,9], "nums2": [17,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [18,10], "nums2": [1,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [18,104], "nums2": [117,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [19,2], "nums2": [1,19] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [19,5], "nums2": [15,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [19,5], "nums2": [52,10] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [19,15], "nums2": [12,19] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [19,15], "nums2": [18,4] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [20,1], "nums2": [1,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [20,5], "nums2": [2,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [20,5], "nums2": [14,8] }
assert my_solution.maxNonDecreasingLength(**test_input) == 1

test_input = { "nums1": [20,7], "nums2": [12,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [20,12], "nums2": [2,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [20,16], "nums2": [8,5] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [20,18], "nums2": [18,20] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [25,83], "nums2": [28,18] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [26,47], "nums2": [87,26] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [28,41], "nums2": [87,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [36,53], "nums2": [66,3] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2

test_input = { "nums1": [44,34], "nums2": [53,44] }
assert my_solution.maxNonDecreasingLength(**test_input) == 2