Dp -> Memoization -> O(n)
Code\n\n\nint f(int i,int flag,vector<int>&nums1,vector<int>&nums2,int n,vector<vector<int>>&dp)\n{\n    if(i == n)\n    {\n        return 0;\n    }\n    if(dp[
# Code\n```\n\nint f(int i,int flag,vector<int>&nums1,vector<int>&nums2,int n,vector<vector<int>>&dp)\n{\n    if(i == n)\n    {\n        return 0;\n    }\n    if(dp[i][flag] != -1)\n        return dp[i][flag];\n    if(flag == 0)\n    {\n        int pick1 = f(i+1,1,nums1,nums2,n,dp) + 1;\n        int pick2 = f(i+1,2,nums1,nums2,n,dp) + 1;\n        int notpick = f(i+1,0,nums1,nums2,n,dp);\n        return dp[i][flag] = max({pick1,pick2,notpick});\n    }\n    else if(flag == 1)\n    {\n        int pick1 = 0;\n        int pick2 = 0;\n        if(nums1[i-1] <= nums1[i])\n            pick1 = f(i+1,1,nums1,nums2,n,dp) + 1;\n        if(nums1[i-1] <= nums2[i])\n            pick2 = f(i+1,2,nums1,nums2,n,dp) + 1;\n        return dp[i][flag] = max(pick1,pick2);\n    }\n    else if(flag == 2)\n    {\n        int pick1 = 0;\n        int pick2 = 0;\n        if(nums2[i-1] <= nums1[i])\n            pick1 = f(i+1,1,nums1,nums2,n,dp) + 1;\n        if(nums2[i-1] <= nums2[i])\n            pick2 = f(i+1,2,nums1,nums2,n,dp) + 1;\n        return dp[i][flag] = max(pick1,pick2);\n    }\n    return dp[i][flag] = 0;\n}\n\n\n\n\nclass Solution {\npublic:\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n        int flag = 0;\n        int n = nums1.size();\n        vector<vector<int>>dp(n,vector<int>(3,-1));\n        return f(0,flag,nums1,nums2,n,dp);\n    }\n};\n```
['C++']