Python3 DP solution with explanation
Intuition\n Describe your first thoughts on how to solve this problem. \nThe intuition behind this solution is that whenever our current value is greater than o
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe intuition behind this solution is that whenever our current value is greater than or equal to the previous value, we extend the length of our non-decreasing subarray by 1. If our current value is smaller than the previous value, we need to restart the length count of our subarray by restarting from length 1. Since the smallest length of our array is 1 in this problem, the smallest non-decreasing subarray is 1. \n\nFor each current position i , we need to compare it to the value in the previous position of both arrays nums1 and nums2. This is because at current position, we need to know whether it is better to continue the previous subarray or restart the subarray from current position. \n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n    def maxNonDecreasingLength(self, nums1: List[int], nums2: List[int]) -> int:\n        N = len(nums1)\n        res = pre1 = pre2 = 1\n\n        def getLen(curArr, i, pre1, pre2):\n            cur = 1\n            if curArr[i] >= nums1[i - 1]:\n                cur = pre1 + 1\n            if curArr[i] >= nums2[i - 1]:\n                cur = max(cur, pre2 + 1)\n\n            return cur\n\n        for i in range(1, N):\n            tmp = pre1\n            pre1 = getLen(nums1, i, pre1, pre2)\n            pre2 = getLen(nums2, i, tmp, pre2)\n            res = max(res, pre1, pre2)\n            #print(f"i = {i} pre1 = {pre1} pre2 = {pre2} res = {res}")\n        return res\n```
['Python3']