C++ Recursion + Memo [DP]
IntuitionTry both nos.Complexity
Time complexity: O(n)

Space complexity: O(n)
Code
# Intuition
Try both nos.


# Complexity
- Time complexity: $$O(n)$$
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity: $$O(n)$$
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```cpp []
class Solution {
public:
    int dp[100001][2];
    int recurs(vector<int>& nums1, vector<int>& nums2,int i,int curr){
        int n = nums1.size();
        if(i==n-1)
            return 0;
        
        if(dp[i][curr]!=-1)
            return dp[i][curr];

        int ans = 0;
        if(curr == 0){
            if(nums1[i]<=nums1[i+1])
                ans=max(ans,recurs(nums1,nums2,i+1,0)+1);
            if(nums1[i]<=nums2[i+1])
                ans=max(ans,recurs(nums1,nums2,i+1,1)+1);
        } else{
            if(nums2[i]<=nums1[i+1])
                ans=max(ans,recurs(nums1,nums2,i+1,0)+1);
            if(nums2[i]<=nums2[i+1])
                ans=max(ans,recurs(nums1,nums2,i+1,1)+1);
        }
        return dp[i][curr]=ans;
    }
    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {
        int ans = 0;
        memset(dp,-1,sizeof(dp));

        for(int i=0;i<nums1.size();i++){
            ans = max(ans,recurs(nums1,nums2,i,0)+1);
            ans = max(ans,recurs(nums1,nums2,i,1)+1);
        }
        return ans;
    }
};
```
['C++']