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


    def __init__(self):
        self.heap = [None]

    def up_heap(self, index):
        if index <= 1:
            return
        parent_index = index // 2
        # print(self.heap[parent_index][0])
        if self.heap[parent_index][0] < self.heap[index][0]:
            self.heap[index], self.heap[parent_index] = self.heap[parent_index], self.heap[index]
            self.up_heap(parent_index)
    
    def down_heap(self, index):
        left_index = index * 2
        right_index = index * 2 + 1
        biggest_index = index

        if left_index < len(self.heap) and self.heap[biggest_index][0] < self.heap[left_index][0]:
            biggest_index = left_index

        if right_index < len(self.heap) and self.heap[biggest_index][0] < self.heap[right_index][0]:
            biggest_index = right_index

        if biggest_index != index:
            self.heap[biggest_index], self.heap[index] = self.heap[index], self.heap[biggest_index]
            self.down_heap(biggest_index)
        
    def insert(self, value, index):
        self.heap.append((value, index))
        self.up_heap(len(self.heap)-1)

    def pop(self):
        if len(self.heap) <= 1:
            return None
        if len(self.heap) == 2:
            return self.heap.pop()

        max_value, max_index = self.heap[1]
        self.heap[1] = self.heap.pop()
        self.down_heap(1)

        return max_value, max_index
        

    def max(self):
        if len(self.heap) <= 1:
            return None
        return self.heap[1]

    def size(self):
        return len(self.heap) - 1

class Solution:
    def maxSlidingWindow(self, nums: List[int], k: int) -> List[int]:
        answer = []
        max_heap = MaxHeap()
        i = 0
        for v in nums:
            max_heap.insert(v, i)
            i += 1
            if i >= k:
                smallest_window_index = i - k
                print(f"{smallest_window_index}-{i}")
                while max_heap.size() > 0:
                    max_value, max_index = max_heap.max()
                    if max_index < smallest_window_index:
                        max_heap.pop()
                    else:
                        answer.append(max_value)
                        break
        return answer


33993