<p>You are given two <strong>0-indexed</strong> integer arrays <code>nums1</code> and <code>nums2</code> of length <code>n</code>.</p>

<p>Let&#39;s define another <strong>0-indexed</strong> integer array, <code>nums3</code>, of length <code>n</code>. For each index <code>i</code> in the range <code>[0, n - 1]</code>, you can assign either <code>nums1[i]</code> or <code>nums2[i]</code> to <code>nums3[i]</code>.</p>

<p>Your task is to maximize the length of the <strong>longest non-decreasing subarray</strong> in <code>nums3</code> by choosing its values optimally.</p>

<p>Return <em>an integer representing the length of the <strong>longest non-decreasing</strong> subarray in</em> <code>nums3</code>.</p>

<p><strong>Note: </strong>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums1 = [2,3,1], nums2 = [1,2,1]
<strong>Output:</strong> 2
<strong>Explanation: </strong>One way to construct nums3 is: 
nums3 = [nums1[0], nums2[1], nums2[2]] =&gt; [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.</pre>

<p><strong class="example">Example 2:</strong></p>

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

<p><strong class="example">Example 3:</strong></p>

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

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums1.length == nums2.length == n &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums1[i], nums2[i] &lt;= 10<sup>9</sup></code></li>
</ul>

class Solution:
    def maxNonDecreasingLength0(self, nums1: List[int], nums2: List[int]) -> int:
        n = len(nums1)
        dp = [[0, 0] for _ in range(n)]
        dp[0] = [1, 1]
        for i in range(1, n):
            if nums1[i] >= nums1[i - 1]:
                dp[i][0] = dp[i - 1][0] + 1
            if nums1[i] >= nums2[i - 1]:
                dp[i][0] = max(dp[i][0], dp[i - 1][1] + 1)
            if nums2[i] >= nums2[i - 1]:
                dp[i][1] = dp[i - 1][1] + 1
            if nums2[i] >= nums1[i - 1]:
                dp[i][1] = max(dp[i][1], dp[i - 1][0] + 1)
        return max(max(row) for row in dp)

    def maxNonDecreasingLength1(self, nums1: List[int], nums2: List[int]) -> int:
        def dfs(previous, i, length):
            if i >= len(nums1):
                return length
            key = (previous, i)
            if key in memo:
                return memo[key]
            v1 = nums1[i]
            v2 = nums2[i]
            minv = min(v1, v2)            
            if previous <= minv:
                memo[key] = dfs(minv, i + 1, length + 1)
                return memo[key]
            maxv = max(v1, v2)
            length1 = dfs(minv, i + 1, 1)
            length2 = dfs(maxv, i + 1, length + 1) if previous <= maxv else length
            memo[key] = max(length1, length2)
            return memo[key]
        memo = {}
        return dfs(0, 0, 0)

    def maxNonDecreasingLength2(self, nums1: List[int], nums2: List[int]) -> int:
        def dfs(next_value, i, length):
            if i < 0:
                return length
            key = (next_value, i)
            if key in memo:
                return memo[key]
            v1 = nums1[i]
            v2 = nums2[i]
            maxv = max(v1, v2)            
            if next_value >= maxv:
                memo[key] = dfs(maxv, i - 1, length + 1)
                return memo[key]
            
            minv = min(v1, v2)
            length1 = dfs(maxv, i - 1, 1)
            length2 = dfs(minv, i - 1, length + 1) if next_value >= minv else length
            memo[key] = max(length1, length2)
            return memo[key]

        memo = {}
        n = len(nums1)
        return dfs(0, n - 1, 0)

    def maxNonDecreasingLength4(self, nums1, nums2):
        def dp(i, prev):
            if i == len(nums1):
                return 0

            key = i, prev
            if key in memo:
                return memo[key]
            res = 0
            if not prev:
                res = dp(i + 1, prev)

            if prev <= nums1[i]:
                res = max(res, 1 + dp(i + 1, nums1[i]))
            if prev <= nums2[i]:
                res = max(res, 1 + dp(i + 1, nums2[i]))
            memo[key] = res
            return res
        memo = {}
        return dp(0, 0)

    def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int:
        return self.maxNonDecreasingLength4(nums1, nums2)
165087