C++, Space complexity O(1)
Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \nremember last two value
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nremember last two value: previous min, and previous max\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n))\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(1)\n# Code\n```cpp []\nclass Solution {\npublic:\n    int maxNonDecreasingLength(vector<int>& nums1, vector<int>& nums2) {\n        int n = int(nums1.size());\n        int preMin=1,preMax=1;\n\n        int r = 1;\n        for (int i = 1; i < n; ++i)\n        {\n            int maxv2 = max(nums1[i], nums2[i]), maxv1 = max(nums1[i - 1], nums2[i - 1]);\n            int minv2 = min(nums1[i], nums2[i]), minv1 = min(nums1[i - 1], nums2[i - 1]);\n\n            int newPreMax,newPreMin;\n\n            if (maxv2 >= maxv1)\n                newPreMax = max(preMax, preMin)+1;\n            else if (maxv2 >= minv1)\n                newPreMax = preMin + 1;\n            else\n                newPreMax = 1;\n\n            if (minv2 >= maxv1)\n                newPreMin = max(preMax, preMin)+1;\n            else if (minv2 >= minv1)\n                newPreMin = preMin + 1;\n            else\n                newPreMin = 1;\n\n            r = max(r, max(newPreMax, newPreMin));\n\n            preMax = newPreMax;\n            preMin = newPreMin;\n        }\n\n        return r;\n    }\n\n};\n```
['C++']