<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:
    """
    sliding window of size k
    very left of the array to the very right
    You can only see the k numbers in the window. 
    Each time the sliding window moves right by one position.

    nums.length large [1, 100000]

    nums value medium [-10000, 10000]

    k [1, nums.length]

    only need to consider next new element in the window.
    if the newest element is bigger than all the others, we can just remove all
    other elements from consideration 

    ex 1: [1 3 -1] -> [3 -1 -3] -> [-1 5 5] -> [5 5 3] -> [5 6 6] -> [6 7 7] 

    indexes 2, 2, 4, 4, 6, 7
    [2, 3] -> [2, 4] -> [4] -> [4, 5] -> [6] -> [7]
    keep track of indexes, if 

    for each window, append max to list
    """
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        max_window: List[int] = []
        max_current_elems = deque([])

        def slide_deque(new_index: int) -> None:
            nonlocal max_current_elems

            # Empty deque, just add
            if not max_current_elems:
                max_current_elems.append(new_index)
                return

            # Remove index if out of window, we are only ever popping 1 element after all
            # Any indexes smaller than this one should have already been filtered out
            if max_current_elems[0] < (new_index - (k - 1)):
                max_current_elems.popleft() 

            # If any elements before the new value are smaller, remove them
            new_value: int = nums[new_index]
        
            while max_current_elems and nums[max_current_elems[-1]] <= new_value:
                max_current_elems.pop()

            # Add the new element to the end of the current elems deque
            max_current_elems.append(new_index)
            
        for i in range(len(nums)):
            # Building the deque in the first k - 1 iterations
            if i < (k - 1):
                slide_deque(i)
                continue

        
            # Otherwise, slide and take the max element
            slide_deque(i)
            max_window.append(nums[max_current_elems[0]])

        return max_window
32413