<p>Given an integer array <code>nums</code>, partition it into two (contiguous) subarrays <code>left</code> and <code>right</code> so that:</p>

<ul>
	<li>Every element in <code>left</code> is less than or equal to every element in <code>right</code>.</li>
	<li><code>left</code> and <code>right</code> are non-empty.</li>
	<li><code>left</code> has the smallest possible size.</li>
</ul>

<p>Return <em>the length of </em><code>left</code><em> after such a partitioning</em>.</p>

<p>Test cases are generated such that partitioning exists.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> nums = [5,0,3,8,6]
<strong>Output:</strong> 3
<strong>Explanation:</strong> left = [5,0,3], right = [8,6]
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,1,1,0,6,12]
<strong>Output:</strong> 4
<strong>Explanation:</strong> left = [1,1,1,0], right = [6,12]
</pre>

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

<ul>
	<li><code>2 &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
	<li>There is at least one valid answer for the given input.</li>
</ul>

class Solution:
    def partitionDisjoint(self, nums: List[int]) -> int:
        # result = 1
        # stack = [(nums[0],0)]
        # for i in range(1,len(nums)):
        #     if stack[-1][0] >= nums[i]:
        #         result = i+1
        #     else:
        #         stack.append((nums[i],i))
        # print(stack)
        # return result

        # result = 1
        # queue = deque([(nums[0],0)])
        # for i in range(1,len(nums)):
        #     if queue[-1][0] >= nums[i]:
        #         result = i+1
        #     else:
        #         queue.append((nums[i],i))
        # print(queue)
        # return result

        # result = 1
        # left = [-nums[0]]
        # right = [(num, i+1) for i, num in enumerate(nums[1:])]
        # heapify(right)
        # i = 1
        # while len(right) > 1 and right[0][0] < -left[0]:
        #     num, idx = heappop(right)
        #     while i < idx:
        #         heappush(left, -nums[i])
        #         i += 1
        #     heappush(left, -num)
        #     result = max(idx+1, result)
        # return result


        result = 1
        leftMax = nums[0]
        right = [(num, i+1) for i, num in enumerate(nums[1:])]
        heapify(right)
        i = 1
        while len(right) > 1 and right[0][0] < leftMax:
            num, idx = heappop(right)
            while i < idx:
                leftMax = max(nums[i], leftMax)
                i += 1
            result = max(idx+1, result)
        return result
34400