🧠Intuitive DP solution || Easy C++ Commented Code ||
Do upvote if you find it useful!!\n# Complexity\n- Time complexity:O(N)\n Add your time complexity here, e.g. O(n) \n\n- Space complexity:O(N*4)\n Add your spac
### Do upvote if you find it useful!!\n# Complexity\n- Time complexity:O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(N*4)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n        int n=nums1.size();\n        vector<vector<int>> dp(n,vector<int> (4,1));\n        //For every index States are :\n        //i)   min(nums1[idx],nums2[idx]) comes from min(nums1[idx-1],nums2[idx-1]) \n        //ii)  max(nums1[idx],nums2[idx]) comes from min(nums1[idx-1],nums2[idx-1]) \n        //iii) min(nums1[idx],nums2[idx]) comes from max(nums1[idx-1],nums2[idx-1]) \n        //iv)  max(nums1[idx],nums2[idx]) comes from max(nums1[idx-1],nums2[idx-1]) \n        int ans=1;\n        for(int i=1 ; i<n ; i++){\n            if(min(nums1[i],nums2[i])>=min(nums1[i-1],nums2[i-1])){\n                dp[i][0]=max(dp[i][0],1+max(dp[i-1][0],dp[i-1][2]));\n                ans=max(ans,dp[i][0]);\n            }\n            if(max(nums1[i],nums2[i])>=min(nums1[i-1],nums2[i-1])){\n                dp[i][1]=max(dp[i][1],1+max(dp[i-1][0],dp[i-1][2]));\n                ans=max(ans,dp[i][1]);\n            }\n            if(min(nums1[i],nums2[i])>=max(nums1[i-1],nums2[i-1])){\n                dp[i][2]=max(dp[i][2],1+max(dp[i-1][1],dp[i-1][3]));\n                ans=max(ans,dp[i][2]);\n            }\n            if(max(nums1[i],nums2[i])>=max(nums1[i-1],nums2[i-1])){\n                dp[i][3]=max(dp[i][3],1+max(dp[i-1][1],dp[i-1][3]));\n                ans=max(ans,dp[i][3]);\n            }\n        }\n        return ans;\n    }\n};\n```
['Dynamic Programming', 'C++']