<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:
        '''
            Intuition : Binary search with monotonic stack
        '''
        # Find the running totals of the subarray via accumulate function from itertools
        # Accumulate will find the running totals of the subarrays by default, so no added parameter is needed 
        running_totals = list(accumulate(nums))
        # monotonic stack is the next part, which needs a base, in this case 0 and -1 as the total and the indexing
        monotonic_stack = [(0, -1)]
        # finally we need a distribution list to use as the place to accrue the max_length answer 
        length_distributions = []
        # set a last index for general use during processing 
        last_index = float('inf')

        # for running total index, running total in running totals 
        for rt_i, rt in enumerate(running_totals):
            # right bisection returns right most index to maintain monotonicity, for which we want the index, not the value
            monotonic_index = monotonic_stack[bisect_right(monotonic_stack, (rt, last_index)) - 1][1]
            # set ahead of time the monotonic value to your running total  
            monotonic_value = rt 
            # if we're in the list
            if monotonic_index >= 0:
                # get your value plus yourself, less the value there already
                monotonic_value += rt - running_totals[monotonic_index] 
                # then, append your new length 
                # you'll be in range since you need to have at least secured this many by this point in time
                length_distributions.append(1 + length_distributions[monotonic_index])
            else: 
                # otherwise, set your monotonic value as twice your total and append 1 
                # this accounts for singular instances and grows your list to match the above logic 
                monotonic_value += rt 
                length_distributions.append(1)
            
            # now we need to empty the stack as much as we can 
            while monotonic_stack and monotonic_stack[-1][0] >= monotonic_value: 
                monotonic_stack.pop()
            monotonic_stack.append((monotonic_value, rt_i))
        
        # when done, your length distributions last item is the greatest 
        return length_distributions[-1]

45000