Python | O(N) Time, O(1) Space | Greedy approach
Approach\n\nAt any point you effectively have the following options:\n\n1. You can continue extending the subarray with minimum of the 2\n2. You have to start a
# Approach\n\nAt any point you effectively have the following options:\n\n1. You can continue extending the subarray with minimum of the 2\n2. You have to start a new subarray because you cannot accomodate the element. In this case you would always want to use the minimum.\n3. You can extend subarray with maximum or may have to start a new one with minimum.\n\nWe will maintain two subarray infos tracking the last element (which is also the largest) one and the length of the subarrays. One subarray will track if we can extend the elements by adding and other will be responsible for starting a new one if needed.\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n    def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int:\n        n = len(nums1)\n        result = 0\n        high = float("-inf")\n        low = float("-inf")\n        # Len 1 Keeps track of long subarray \n        len1 = 0\n        # Len 2 is the shorter subarray \n        len2 = 0\n        for i in range(n):\n            minval = min(nums1[i], nums2[i])\n            maxval = max(nums1[i], nums2[i])\n            # If maxval is greater than last element of long subarray, we can always increase the long by 1\n            if maxval>=high:\n                len1+=1\n                # If minval is also greater than long subarray last element, we should take that instead\n                if minval>=high:\n                    len2 = len1\n                    low = high = minval\n                # If minval is less than shorter array, we need to discard our short array and start a new one\n                elif minval<low:\n                    high = maxval\n                    len2 = 1\n                    low = minval\n                # If minval can be accomodated in short array, then use that\n                else:\n                    high = maxval\n                    len2+=1\n                    low = minval\n            # If maval is shorter than both subarrays, then discard both the arrays and start new\n            elif maxval<low:\n                len1 = len2 = 1\n                low = high = minval   \n            # If maxval is less than long subarray, we need to discord long subarray\n            # We make the shorter subarray as the long one\n            else:\n                len1 = len2+1\n                # Since short/long array can accomodate the minimum element, we will use it\n                if minval>=low:\n                    len2 = len1\n                    low = high = minval\n                # Otherwise we will use the max element for long array and create a new array for short\n                else:\n                    len2 = 1\n                    high = maxval\n                    low = minval\n            result = max(result, len1, len2)\n      \n        return result\n```
['Python3']