C++ || Recursion +Memoization || Explained
Intuition\nAt every index if both numbers are greater than the last choosen number then we have 2 choices which indicates that applying greedy here will not be 
# Intuition\nAt every index if both numbers are greater than the last choosen number then we have 2 choices which indicates that applying greedy here will not be benificial.We need to explore all possible choices.\nSo how we explore all choices??  RECURSION \n\n# Approach\nWe will maintain a variable last which will indicate the last number was choosen from which array.\n- If last =0 it means last number was choosen from nums1\n- If last =1 it means last number was choosen from nums2\n- If last =-1 it means that we are not continuing with the increasing sequence formed till ind-1 \n\nSo if we have the last number then we can take num1 only if nums1>=last_number and similarly nums2.\n\n\n\n\n\n# Recursive Code [TLE]\n```\nclass Solution {\npublic:\n    \n    \n    int find(int ind,vector<int>&nums1,vector<int>&nums2,int last)\n    {\n        if(ind==nums1.size()) // if we have used all numbers\n        {\n            return 0;\n        }\n        \n        int ans=0;\n        if(last==-1) // we are not continuing with the formed sequence or we are generating the sequence from index 0\n        {\n            ans=max(ans,1+find(ind+1,nums1,nums2,0)); //taking nums1[ind]\n            ans=max(ans,1+find(ind+1,nums1,nums2,1));//taking num2[ind]\n            ans=max(ans,find(ind+1,nums1,nums2,-1));// not taking anyone in the increasing sequence\n        }\n        else\n        {\n            int val=0; // for storing the last_number\n            if(last==0)\n                val=nums1[ind-1];\n            else\n                val=nums2[ind-1];\n            \n            if(nums1[ind]>=val) // checking if we can take nums1\n                ans=max(ans,1+find(ind+1,nums1,nums2,0));\n            \n            if(nums2[ind]>=val)// checking if we can take nums2\n                ans=max(ans,1+find(ind+1,nums1,nums2,1));   \n        }\n        return ans;\n    }\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2)\n    {\n        int ans=find(0,nums1,nums2,-1);// find length of longest increasing if we start from index 0 and last_num was -1\n        return ans;\n    }\n};\n\n```\n# Memoized code [ACCEPTED]\n```\n// here while memoizing we add +1 to last because last is -1 sometimes and there is no index as -1.Adding +1 makes it zero.\nclass Solution {\npublic:\n    \n    \n    int find(int ind,vector<int>&nums1,vector<int>&nums2,int last,vector<vector<int>>&dp)\n    {\n        if(ind==nums1.size())\n        {\n            return 0;\n        }\n        \n        if(dp[ind][last+1]!=-1)\n            return dp[ind][last+1];\n        \n        int ans=0;\n        if(last==-1)\n        {\n            ans=max(ans,1+find(ind+1,nums1,nums2,0,dp));\n            ans=max(ans,1+find(ind+1,nums1,nums2,1,dp));\n            ans=max(ans,find(ind+1,nums1,nums2,-1,dp));\n        }\n        else\n        {\n            int val=0;\n            if(last==0)\n                val=nums1[ind-1];\n            else\n                val=nums2[ind-1];\n            \n            if(nums1[ind]>=val)\n                ans=max(ans,1+find(ind+1,nums1,nums2,0,dp));\n            \n            if(nums2[ind]>=val)\n                ans=max(ans,1+find(ind+1,nums1,nums2,1,dp));   \n        }\n        return dp[ind][last+1]=ans;\n    }\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2)\n    {\n        vector<vector<int>>dp(nums1.size(),vector<int>(3,-1));// Max value of last can be when last_num is from nums2 \n                                                             //so last=1 and add 1 so max last=2\n        int ans=find(0,nums1,nums2,-1,dp);\n        return ans;\n    }\n};\n```\n\n\n\n
['Dynamic Programming', 'Recursion', 'Memoization', 'C++']