Easy DP Solution O(N)...
Intuition\n Describe your first thoughts on how to solve this problem. \nIntuition for this problem was quite obvious, at every index, we have two operations ei
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nIntuition for this problem was quite obvious, at every index, we have two operations either we pick the element from first array or we pick the element from second array. So we can use dp to solve this question.\n# Approach\n<!-- Describe your approach to solving the problem. -->\nI have taken two states one is index which will be used to traverse the array and second is pick, it denotes whether the last element was picked from first array or second array if pick==1 then it means it the last element was picked from first array, if pick==2 then it means the last element was picked from the second array but if pick==0, then it means no element has been picked yet.\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nThe time complexity of dp solution is the product of the states, here in this case value of index will range from (1 to n) and value of pick will range from (0 to 2), so the T.C. would be O(N*2) which will be equal to O(N). \n\n# Code\n```\nclass Solution {\npublic:\n    int solve(int ind, int pick, vector<int>& nums1, vector<int>& nums2,vector<vector<int>> &dp){\n        // if we reached last element return 0\n        if(ind==nums1.size()) return 0;\n\n        int p1=0,p2=0,p3=0;\n\n        // memoize\n        if(dp[ind][pick]!=-1) return dp[ind][pick];\n        \n        // if last element was picked from first array\n        if(pick==1){\n\n            // check if the current element of first array is greater than previous element of first array\n            if(nums1[ind]>=nums1[ind-1]){\n                p1=1+solve(ind+1,1,nums1,nums2,dp);\n            }\n\n            // check if the current element of second array is greater than previous element of last array\n\n            if(nums2[ind]>=nums1[ind-1]){\n                p2=1+solve(ind+1,2,nums1,nums2,dp);\n            }\n\n            // if no condition is satisfied then return because we can\'t take the current element and since we require the longest subarray and subarrays are continous so we can\'t skip any element. \n            if(nums1[ind]<nums1[ind-1]&&nums2[ind]<nums1[ind-1]) return 0;\n            \n        }else if(pick==2){\n\n            // check if the current element of second array is greater than previous element of first array\n            if(nums1[ind]>=nums2[ind-1]){\n                p1=1+solve(ind+1,1,nums1,nums2,dp);\n            }\n\n            // check if the current element of secibd array is greater than previous element of first array\n            if(nums2[ind]>=nums2[ind-1]){\n                p2=1+solve(ind+1,2,nums1,nums2,dp);\n            }\n\n            // no condtion fullfiled return\n            if(nums1[ind]<nums2[ind-1]&&nums2[ind]<nums2[ind-1]) return 0;\n        }else if(pick==0){\n            // if no element has been picked yet \n\n            // pick=1 because we are picking from first array\n            p1=1+solve(ind+1,1,nums1,nums2,dp);\n            \n            // pick=2 because we are picking from second array\n            p2=1+solve(ind+1,2,nums1,nums2,dp);\n\n            // skip the element \n            p3=solve(ind+1,0,nums1,nums2,dp);\n        }\n        return dp[ind][pick]=max(p1,max(p2,p3));\n    }\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n        int n=nums1.size();\n        vector<vector<int>> dp(n,vector<int>(3,-1));\n        return solve(0,0,nums1,nums2,dp);\n    }\n};\n```\n*Please do upvote it if you liked the solution, it really helps* \uD83D\uDE4F\n\nIf there is any doubt in the solution then you can ask in the comment section below..\n\n**Happy leetcoding :)**\n\n
['Dynamic Programming', 'Recursion', 'Memoization', 'C++']