<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
class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        q = deque()
        start = 0
        result = []
        for i in range(k):
            while q and q[-1] < nums[i]:
                q.pop()
            q.append(nums[i])
        result.append(q[0])
        for i in range(1, len(nums)-k+1):
            if q and nums[i-1] == q[0]:
                q.popleft()

            while q and q[-1] < nums[i+k-1]:
                q.pop()
            
            q.append(nums[i+k-1])
            result.append(q[0])
        return result

        # [1,2,1,0,4,2,6]
        # [4,1]
        2,2,4,4,6
        count = {}
        curr_max = float('-inf')
        for c in range(k):
            count[nums[c]] = count.get(nums[c], 0) + 1
            curr_max = max(curr_max, nums[c])
        result = [curr_max]
        for i in range(1, len(nums)-k+1):
            count[nums[i-1]] -= 1
            if count[nums[i-1]] == 0:
                del count[nums[i-1]]
            count[nums[i+k-1]] = count.get(nums[i+k-1], 0) + 1
            if curr_max in count:
                curr_max = max(curr_max, nums[i+k-1])
            else:
                curr_max = max(count)
            result.append(curr_max)
        return result
29648