<p>You are given a <strong>0-indexed</strong> integer array <code>nums</code>.</p>

<p>You can perform any number of operations, where each operation involves selecting a <strong>subarray</strong> of the array and replacing it with the <strong>sum</strong> of its elements. For example, if the given array is <code>[1,3,5,6]</code> and you select subarray <code>[3,5]</code> the array will convert to <code>[1,8,6]</code>.</p>

<p>Return <em>the </em><strong><em>maximum</em></strong><em> length of a </em><strong><em>non-decreasing</em></strong><em> array that can be made after applying operations.</em></p>

<p>A <strong>subarray</strong> is a contiguous <strong>non-empty</strong> sequence of elements within an array.</p>

<p>&nbsp;</p>
<p><strong>Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,2,2]
<strong>Output:</strong> 1
<strong>Explanation:</strong> This array with length 3 is not non-decreasing.
We have two ways to make the array length two.
First, choosing subarray [2,2] converts the array to [5,4].
Second, choosing subarray [5,2] converts the array to [7,2].
In these two ways the array is not non-decreasing.
And if we choose subarray [5,2,2] and replace it with [9] it becomes non-decreasing. 
So the answer is 1.
</pre>

<p><strong>Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4]
<strong>Output:</strong> 4
<strong>Explanation:</strong> The array is non-decreasing. So the answer is 4.
</pre>

<p><strong>Example 3:</strong></p>

<pre>
<strong>Input:</strong> nums = [4,3,2,6]
<strong>Output:</strong> 3
<strong>Explanation:</strong> Replacing [3,2] with [5] converts the given array to [4,5,6] that is non-decreasing.
Because the given array is not non-decreasing, the maximum<!-- notionvc: 3447a505-d1ee-4411-8cae-e52162f53a55 --> possible answer is 3.</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def findMaximumLength(self, nums: List[int]) -> int:
        
        # dp[i] = dp[j] + 1 where sum(nums[j+1:i+1]) >= last[j]. Find max j
        # by construction of finding max j, last[i] is smallest last number of dp[i]
        # we can find sum(nums[j+1:i+1]) as subtraction of prefixSum
        # preSum(i) - preSum(j) >= last[j] => preSum(i) >= preSum(j) + last[j]
        # so we keep an increasing monotonic stack of j where preSum(j) + last[j] is minimum
        dp = [1]
        last = [nums[0]]
        prefixSum = [nums[0]]
        monoStack = deque([])
        monoStack.append((0, nums[0] * 2))
        for i in range(1, len(nums)):
            prefixSum.append(prefixSum[-1] + nums[i])
            while (len(monoStack) > 1 
                and prefixSum[i] >= monoStack[0][1] 
                and prefixSum[i] >= monoStack[1][1]):
                monoStack.popleft() # get largest j
            lastMinIndex, preAndLast = monoStack[0]
            if prefixSum[i] >= preAndLast:
                dp.append(dp[lastMinIndex] + 1)
                last.append(prefixSum[i] - prefixSum[lastMinIndex])
            else:
                dp.append(dp[-1])
                last.append(last[-1] + nums[i])
            curr = prefixSum[i] + last[i]
            while monoStack:
                if monoStack[-1][1] >= curr:
                    monoStack.pop()
                else:
                    break
            monoStack.append((i, curr))
        return dp[-1]



    def original(self, nums):
        i = 0
        ops = 0
        last = nums[0]
        while i < len(nums):
            foldLeft = False
            if last <= nums[i]:
                last = nums[i]
                i += 1
                continue
            target = 0
            j = i
            while j < len(nums) and target < last:
                target += nums[j]
                if j + 1 < len(nums) and nums[j+1] >= target + last:
                    ops += 1
                    last = last + target
                    i = j + 1
                    foldLeft = True
                    break
                if j != i:
                    ops += 1
                j += 1
            if target < last and not foldLeft:
                ops += 1
                break
            i = j
            last = target
        return len(nums) - ops
42400