<p>The <strong>median</strong> is the middle value in an ordered integer list. If the size of the list is even, there is no middle value. So the median is the mean of the two middle values.</p>

<ul>
	<li>For examples, if <code>arr = [2,<u>3</u>,4]</code>, the median is <code>3</code>.</li>
	<li>For examples, if <code>arr = [1,<u>2,3</u>,4]</code>, the median is <code>(2 + 3) / 2 = 2.5</code>.</li>
</ul>

<p>You are given an integer array <code>nums</code> and an integer <code>k</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 median array for each window in the original array</em>. Answers within <code>10<sup>-5</sup></code> of the actual value will be accepted.</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> [1.00000,-1.00000,-1.00000,3.00000,5.00000,6.00000]
<strong>Explanation:</strong> 
Window position                Median
---------------                -----
[<strong>1  3  -1</strong>] -3  5  3  6  7        1
 1 [<strong>3  -1  -3</strong>] 5  3  6  7       -1
 1  3 [<strong>-1  -3  5</strong>] 3  6  7       -1
 1  3  -1 [<strong>-3  5  3</strong>] 6  7        3
 1  3  -1  -3 [<strong>5  3  6</strong>] 7        5
 1  3  -1  -3  5 [<strong>3  6  7</strong>]       6
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> nums = [1,2,3,4,2,3,1,4,2], k = 3
<strong>Output:</strong> [2.00000,3.00000,3.00000,3.00000,2.00000,3.00000,2.00000]
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= nums.length &lt;= 10<sup>5</sup></code></li>
	<li><code>-2<sup>31</sup> &lt;= nums[i] &lt;= 2<sup>31</sup> - 1</code></li>
</ul>

class HashHeap:
    def __init__(self):
        self.pq = [] # [value, nums_idx]
        self.idx_to_idx = {} # nums_idx to pq_idx
    
    def push(self, data):
        val, nums_idx = data
        n = self.size()
        self.pq.append([val, nums_idx])
        self.idx_to_idx[nums_idx] = n
        self.siftup(n)
    
    def pop(self):
        return self.remove(0)
    
    def remove(self, pq_idx):
        n = self.size()
        self.swap(pq_idx, n - 1)
        data = self.pq.pop()
        del self.idx_to_idx[data[1]]
        if pq_idx != n - 1:
            self.siftup(pq_idx)
            self.siftdown(pq_idx)
        return data
    
    def siftup(self, pq_idx):
        while pq_idx > 0:
            parent = (pq_idx - 1) // 2
            if self.pq[pq_idx] < self.pq[parent]:
                self.swap(pq_idx, parent)
                pq_idx = parent
            else:
                break
    
    def swap(self, idx1, idx2):
        self.pq[idx1], self.pq[idx2] = self.pq[idx2], self.pq[idx1]
        nums_idx1, nums_idx2 = self.pq[idx1][1], self.pq[idx2][1]
        self.idx_to_idx[nums_idx1] = idx1
        self.idx_to_idx[nums_idx2] = idx2
    
    def siftdown(self, pq_idx):
        while pq_idx < self.size():
            left = pq_idx * 2 + 1
            right = pq_idx * 2 + 2
            min_idx = pq_idx
            if left < self.size() and self.pq[left] < self.pq[min_idx]:
                min_idx = left
            if right < self.size() and self.pq[right] < self.pq[min_idx]:
                min_idx = right
            if pq_idx != min_idx:
                self.swap(pq_idx, min_idx)
                pq_idx = min_idx
            else:
                break
    
    def size(self):
        return len(self.pq)
    
    def get_top_val(self):
        return self.pq[0][0]
    
    


class Solution:
    def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]:
        if not nums or len(nums) < k:
            return []
        min_heap, max_heap = HashHeap(), HashHeap()
        ans = []
        for nums_idx, num in enumerate(nums):
            self.push(min_heap, max_heap, num, nums_idx)
            if nums_idx >= k:
                self.remove(min_heap, max_heap, nums_idx - k)
            self.balance(min_heap, max_heap)
            if nums_idx >= k - 1:
                ans.append(self.get_median(min_heap, max_heap))
        return ans

    def push(self, min_heap, max_heap, num, nums_idx):
        if max_heap.size() == 0 or num < -max_heap.get_top_val():
            max_heap.push([-num, nums_idx])
        else:
            min_heap.push([num, nums_idx])
    
    def balance(self, min_heap, max_heap):
        while max_heap.size() > min_heap.size() + 1:
            neg_num, nums_idx = max_heap.pop()
            min_heap.push([-neg_num, nums_idx])
        while min_heap.size() > max_heap.size():
            num, nums_idx = min_heap.pop()
            max_heap.push([-num, nums_idx])
    
    def remove(self, min_heap, max_heap, nums_idx):
        if nums_idx in max_heap.idx_to_idx:
            pq_idx = max_heap.idx_to_idx[nums_idx]
            max_heap.remove(pq_idx)
        else:
            pq_idx = min_heap.idx_to_idx[nums_idx]
            min_heap.remove(pq_idx)

    def get_median(self, min_heap, max_heap):
        if max_heap.size() == 0 and min_heap.size() == 0:
            return None
        if max_heap.size() > min_heap.size():
            return -max_heap.get_top_val()
        else:
            return 0.5 * (-max_heap.get_top_val() + min_heap.get_top_val())

        
35534