Self explanatory || C++ || 2D dp
Make  a 2D dp array dp[i][j], \ni -> current index\'s best answer\nj -> 0 if we selected nums1, 1 if we selected nums2\n\nTo reduce the time complexity, memoize
Make  a 2D dp array dp[i][j], \ni -> current index\'s best answer\nj -> 0 if we selected nums1, 1 if we selected nums2\n\nTo reduce the time complexity, memoize this.\nNow the answer you will get is at index 0. So what we do is we will check for all indices. But this won\'t increase the time complexity, since all the answer was already stored in the dp array.\n\nRest is self-explanatory. Just imagine what you will do when you do it yourself with pen-paper.\n\n```\nclass Solution {\npublic:\n    int solve(vector<vector<int>>& dp, vector<int>& nums1, vector<int>& nums2, int idx, bool choice){\n        int n=nums1.size();\n        if(idx==n) return 0;\n        if(dp[idx][choice] != -1) return dp[idx][choice];\n        int ans = 1, curr = choice ? nums2[idx] : nums1[idx];\n        if(idx+1 < n){\n            if(curr <= nums1[idx+1]) ans = 1 + solve(dp, nums1, nums2, idx+1, 0);\n            if(curr <= nums2[idx+1]) ans = max(ans, 1 + solve(dp, nums1, nums2, idx+1, 1));\n        }\n        return dp[idx][choice] = ans;\n    }\n    \n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n        int n=nums1.size(), ans=0;\n        vector<vector<int>> dp(n, vector<int>(2, -1));\n        for(int i=0; i<n; i++){\n            ans = max({ans, solve(dp, nums1, nums2, i, 0), solve(dp, nums1, nums2, i, 1)});\n        }\n        return ans;\n    }\n};\n```
['Dynamic Programming', 'Memoization', 'C']