🔏 Key Insight
Intuition\n\nThe key insight is that we want to greedily maximize the length of the longest non-decreasing subarray at each step. This suggests using dynamic pr
## Intuition\n\nThe key insight is that we want to greedily maximize the length of the longest non-decreasing subarray at each step. This suggests using dynamic programming where we make optimal decisions at each index to maximize the length.\n\n## Approach\n\nWe use a recursive function `dp(i, prev)` that represents the longest non-decreasing subarray we can make starting at index `i`, where `prev` is the previous element added.\n\nThe base case is when we reach the end of the arrays, in which case the length is 0.\n\nFor each index `i`, we consider two choices:\n\n1. Take `nums1[i]`. This is only valid if `nums1[i] >= prev`, in which case we can extend the previous subarray by 1.\n2. Take `nums2[i]`. This is only valid if `nums2[i] >= prev`, in which case we can also extend the previous subarray by 1.\n\nWe make the optimal choice that gives the maximum subarray length. The final result is `dp(0, -inf)` starting from the beginning with no previous element.\n\n## Complexity\nLet $n$ be the length of the arrays:\n\n- Time complexity: $O(n)$\n    + We recurse through each index once.\n\n- Space complexity: $O(n)$\n    + Our recursion stack can go up to $n$ levels deep.\n\n## Code\n```python []\nclass Solution:\n    def maxNonDecreasingLength(self, nums1, nums2):\n        @cache\n        def dp(i, prev):\n            if i == len(nums1):\n                return 0\n\n            res = 0\n            if not prev:\n                res = dp(i + 1, prev)\n\n            if prev <= nums1[i]:\n                res = max(res, 1 + dp(i + 1, nums1[i]))\n            if prev <= nums2[i]:\n                res = max(res, 1 + dp(i + 1, nums2[i]))\n\n            return res\n\n        return dp(0, 0)\n```
['Dynamic Programming', 'Recursion', 'Memoization', 'Python3']