C++ || Brute Force Recursion , Memoization and Tabulation
Intuition\n Describe your first thoughts on how to solve this problem. \nThis is a simple pick and not pick pattern. \n\n# Approach\n Describe your approach to 
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThis is a simple pick and not pick pattern. \n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIf it is safe to pick from array nums1(nums1[i] >= prev) we pick the element and find the ans , similarly from array nums2. We might also get a longer sequence further if we dont pick from either \n\n# Recursion\n```\nclass Solution {\npublic:\n    int maxDec(vector<int>& nums1, vector<int>& nums2, int prev ,int i){\n        int n = nums1.size();\n        if(i >= n){\n            return 0;\n        }\n        int nopick = 0;     \n         int pick1 = 0;\n        int pick2 = 0;\n        if(prev == -1){\n        nopick = maxDec(nums1,nums2,prev,i+1);\n        }\n  \n        if(nums1[i] >= prev){\n            pick1 = 1 + maxDec(nums1,nums2,nums1[i],i+1);\n        }\n        if(nums2[i] >= prev){\n                pick2 = 1 + maxDec(nums1,nums2,nums2[i],i+1);\n        }\n        return max({pick1,pick2,nopick});\n    }\n\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n        // this is a simple pick not pick pattern \n        int n = nums1.size();\n       int prev = -1;\n        return maxDec(nums1,nums2,prev,0);\n    }\n};\n```\n# Memorization\n```\nclass Solution {\npublic:\nint dp[100003][3];\n    int maxDec(vector<int>& nums1, vector<int>& nums2, int prev ,int i){\n        int n = nums1.size();\n        if(i >= n){\n            return 0;\n        }\n        if(dp[i][prev] != -1){\n            return dp[i][prev];\n        }\n        int nopick = 0;     \n        int pick1 = 0;\n        int pick2 = 0;\n        if(prev == 0){\n        nopick = maxDec(nums1,nums2,prev,i+1);\n        }\n        int val;\n        if(prev == 0){\n            val = 0; // in this case nums1[i] > val and nums2[i] > val\n        }\n        else if(prev == 1){\n            val = nums1[i-1];\n        }\n        else{\n            val = nums2[i-1];\n        }\n        if(nums1[i] >= val){\n            pick1 = 1 + maxDec(nums1,nums2,1,i+1);\n        }\n        if(nums2[i] >= val){\n                pick2 = 1 + maxDec(nums1,nums2,2,i+1);\n        }\n        return dp[i][prev] = max({pick1,pick2,nopick});\n    }\n\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n        // this is a simple pick not pick pattern \n        int n = nums1.size();\n       int prev = -1;\n       // now see while doing dp we cannot store prev bcoz prev might range till 1e9 \n       // so we will store prev -> 0 subarray size is 0 \n       // prev-> 1 last element from nums1\n       // prev -> 2 last element from nums2 \n        memset(dp,-1,sizeof(dp));\n        return maxDec(nums1,nums2,0,0);\n    }\n};\n```\n# Tabulation\n```\nclass Solution {\npublic:\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n         int n = nums1.size();\n         // we store dp with all 1\'s as min size of subarray will be 1\n        vector<vector<int>> dp(n+1,vector<int>(2,1)); \n        int ans = 1;\n        if(n==1) return ans;\n\n        for(int i=1;i<n;i++){\n            if (nums2[i] >= nums2[i - 1])\n                dp[i][1] = dp[i - 1][1] + 1;\n            if (nums2[i] >= nums1[i - 1])\n                dp[i][1] = max(dp[i][1], dp[i - 1][0] + 1); \n            if (nums1[i] >= nums1[i - 1])\n                dp[i][0] = dp[i - 1][0] + 1;\n            if (nums1[i] >= nums2[i - 1])\n                dp[i][0] = max(dp[i][0], dp[i - 1][1] + 1);\n        }\n        \n        for(int i=0;i<n;i++){\n            ans = max({ans,dp[i][0],dp[i][1]});\n        }\n        return ans;\n    }\n};\n```
['Dynamic Programming', 'Recursion', 'Memoization', 'C++']