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

import heapq
from collections import defaultdict
class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        if len(nums) == 1:
            return [nums[0]]
        # assumptions will len(nums) >= k?
        # will nums be valid inputs?
        # ints, floats? 
        # range of these numbers?
        # range of k?

        heap = [-nums[i] for i in range(k)]
        heapq.heapify(heap)
        
        # first idea is just have a heap that keeps track of the max
        # pop that max from the heap once it leaves
        # problem its hard to pop the 2nd biggest item in 
        # keep track of nums to pop in a hashmap

        nums_to_pop = defaultdict(int) #{num: how many}
        max_in_window = [-heap[0]]
        # pop {-1: 1, -3: 1}
        # heap [-1, -3, 1, 3, -5]
        # output: 3, 3, 5, 
        # rn edge case if i == 1: solved
        for i in range(1, len(nums) - k + 1):
            nums_to_pop[-nums[i - 1]] += 1
            heapq.heappush(heap, -nums[i + k - 1])

            while nums_to_pop[heap[0]] > 0:
                nums_to_pop[heap[0]] -= 1
                heapq.heappop(heap)
            max_in_window.append(-heap[0])

        return max_in_window






        



38536