Clean and easy | C++ solution
Approach\nI used a 2D DP array to store the longest subarray at the ith index, formed by considering both the minimum and maximum values from either nums1 or nu
# Approach\nI used a 2D DP array to store the longest subarray at the `ith` index, formed by considering both the `minimum` and `maximum` values from either `nums1` or `nums2`. In the DP array, the length of the subarray formed by the minimum element is stored at the `0th` index, while the subarray formed by the maximum element is stored at the `1st` index.\n\nTo achieve our result, we iterate over the elements of nums1 and nums2 starting from index 1. At each index, we compare the current element with the previous element from both vectors. Based on these comparisons, we update the dp array, considering two possibilities: \n- either the current element is greater than or equal to the previous element, or it is smaller.\n\nFinally, the code updates the maximum non-decreasing length, `nax`, by comparing it with the values in the dp array at each index. The function returns `nax + 1`, which represents the maximum non-decreasing length of the combined sequence of nums1 and nums2.\n\n# Code\n```\nclass Solution {\npublic:\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n        int n = nums1.size();\n        int nax = 0;\n        vector<vector<int>> dp(n, vector<int>(2, 0));\n        \n        for (int i = 1; i < n; ++i) {\n            int imin = min(nums1[i], nums2[i]);\n            int imax = max(nums1[i], nums2[i]);\n            int jmin = min(nums1[i - 1], nums2[i - 1]);\n            int jmax = max(nums1[i - 1], nums2[i - 1]);\n            \n            if (imin >= jmin)\n                dp[i][0] = max(dp[i - 1][0] + 1, dp[i][0]);\n            if (imin >= jmax)\n                dp[i][0] = max(dp[i - 1][1] + 1, dp[i][0]);\n            \n            if (imax >= jmin)\n                dp[i][1] = max(dp[i - 1][0] + 1, dp[i][1]);\n            if (imax >= jmax)\n                dp[i][1] = max(dp[i - 1][1] + 1, dp[i][1]);\n            \n            nax = max(nax, dp[i][0]);\n            nax = max(nax, dp[i][1]);\n        }\n        \n        return nax + 1;\n    }\n};\n\n```
['Dynamic Programming', 'C++']