<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>

class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        # Brute force: As the window moves, rescan the whole window to find the maximum.
        # Slightly better brute force: As you move along the nums, maintain a check for the current maximum
        # If the current maximum is removed, then rescan the entire window for the new maximum
        # Keep count of all the numbers in the list, and maxheap that maintains the maximum numbers
        # Once the current max is popped, then keep popping until you find a number that is still in the dictionary

        # Dictionary to track counts of characters in current window
        nDict = {}

        # Heap to track the maximum value within the current window
        maxHeap = []

        # Prepare result array
        res = []

        # Start by instantiating nDict
        for i in range(k):
            nDict[nums[i]] = nDict.get(nums[i], 0) + 1
            heapq.heappush(maxHeap, -nums[i])

        # print(f"strating heap: {maxHeap}")
        # print(f"starting nDict: {nDict}")

        # Append first result
        res.append(-maxHeap[0])

        # Now looop through teh whole list
        l = 0
        for r in range(k, len(nums)):
            # Add incoming number
            nDict[nums[r]] = nDict.get(nums[r], 0) + 1
            heapq.heappush(maxHeap, -nums[r])

            # Remove start number
            nDict[nums[l]] = nDict.get(nums[l], 0) - 1

            # CHeck if removed number was the maximum
            if nums[l] == -maxHeap[0]:
                # print(f"{nums[l]} found to be equal to top of maxheap: {-maxHeap[0]}")
                # print(f"nDict: {nDict}")
                while -maxHeap[0] not in nDict or nDict[-maxHeap[0]] == 0:
                    # Pop from the top
                    print(f'Popped {heapq.heappop(maxHeap)}')
                    # print(f'Updated heap: {maxHeap}')

            res.append(-maxHeap[0])

            l += 1


        return res
36956