<p>You are given an array of integers&nbsp;<code>nums</code>, there is a sliding window of size <code>k</code> which is moving from the very left of the array to the very right. You can only see the <code>k</code> numbers in the window. Each time the sliding window moves right by one position.</p>

<p>Return <em>the max sliding window</em>.</p>

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

<pre>
<strong>Input:</strong> nums = [1,3,-1,-3,5,3,6,7], k = 3
<strong>Output:</strong> [3,3,5,5,6,7]
<strong>Explanation:</strong> 
Window position                Max
---------------               -----
[1  3  -1] -3  5  3  6  7       <strong>3</strong>
 1 [3  -1  -3] 5  3  6  7       <strong>3</strong>
 1  3 [-1  -3  5] 3  6  7      <strong> 5</strong>
 1  3  -1 [-3  5  3] 6  7       <strong>5</strong>
 1  3  -1  -3 [5  3  6] 7       <strong>6</strong>
 1  3  -1  -3  5 [3  6  7]      <strong>7</strong>
</pre>

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

<pre>
<strong>Input:</strong> nums = [1], k = 1
<strong>Output:</strong> [1]
</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>-10<sup>4</sup> &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

from collections import deque
import heapq
class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        q = deque()
        heap = []
        ans = []
        if k == 1:
            return nums

        max = float('-inf')
        for j, i in enumerate(nums):
            if len(q) < k:
                q.append(i)
                if i > max:
                    max = i
                    heap = []
                else:
                    heapq.heappush(heap, (-1*i, j))
            else:
                ans.append(max)
                pop = q.popleft()
                if pop == max:
                    pop, index = heapq.heappop(heap)
                    max = pop*-1
                    while heap:
                        if heap[0][1] < index:
                            heapq.heappop(heap)
                        else:
                            break
                q.append(i)
                if i > max:
                    heap = []
                    max = i
                else:
                    heapq.heappush(heap, (-1*i, j))
        ans.append(max)
        return ans

        

# 1 3 -1 : 3 : -1 
# 3 -1 -3 : 3 : -1 -3
# -1 -3 5 : if next one is larger, clear heap 

# -7 -8 7 5 : 7 : 5
# -8 7 5 7 : 7 : 
# 7 5 7 1 : 7 : 1
# 5 7 1 6 : 7 : 6 1

# 3 8 3 3 3 6 3 7 3 4 3 3 3 3 
# 3 8 3 3 3 6 3 7 : 8 : 7 6 
# 3 3 3 6 3 7 3 4 : 7 : 6 4
37153