<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 MedianFinder:
    def __init__(self, k):
        self.min_heap = []
        self.max_heap = []
        self.min_heap_delete = defaultdict(int)
        self.min_heap_delete_count = 0
        self.max_heap_delete = defaultdict(int)
        self.max_heap_delete_count = 0

    def rebalance(self):
        while self.get_min_heap_size() > self.get_max_heap_size() + 1:
            heapq.heappush(self.max_heap, -self.pop_from_min_heap())
        while self.get_min_heap_size() < self.get_max_heap_size():
            heapq.heappush(self.min_heap, -self.pop_from_max_heap())

    def add_value(self, num):
        if not self.min_heap or self.min_heap[0] <= num:
            heapq.heappush(self.min_heap, num)
        else:
            heapq.heappush(self.max_heap, -num)
        self.rebalance()

    def pop_from_min_heap(self):
        val = heapq.heappop(self.min_heap)
        while self.min_heap and self.min_heap_delete[self.min_heap[0]] > 0:
            self.min_heap_delete[self.min_heap[0]] -= 1
            self.min_heap_delete_count -= 1
            heapq.heappop(self.min_heap)
        self.rebalance()
        return val

    def pop_from_max_heap(self):
        val = heapq.heappop(self.max_heap)
        while self.max_heap and self.max_heap_delete[self.max_heap[0]] > 0:
            self.max_heap_delete[self.max_heap[0]] -= 1
            self.max_heap_delete_count -= 1
            heapq.heappop(self.max_heap)
        self.rebalance()
        return val

    def get_min_heap_size(self):
        return len(self.min_heap) - self.min_heap_delete_count

    def get_max_heap_size(self):
        return len(self.max_heap) - self.max_heap_delete_count

    def remove_value(self, num):
        if num == self.min_heap[0]:
            self.pop_from_min_heap()
        elif -num == self.max_heap[0]:
            self.pop_from_max_heap()
        elif num > self.min_heap[0]:
            self.min_heap_delete[num] += 1
            self.min_heap_delete_count += 1
        else:
            self.max_heap_delete[-num] += 1
            self.max_heap_delete_count += 1
        self.rebalance()

    def find_median(self):
        # print(self.min_heap)
        # print(self.min_heap_delete)
        # print(self.min_heap_delete_count)
        # print(self.max_heap)
        # print(self.max_heap_delete)
        # print(self.max_heap_delete_count)
        # print()
        if self.get_min_heap_size() == self.get_max_heap_size():
            return (self.min_heap[0] - self.max_heap[0]) * 0.5
        return self.min_heap[0] * 1.0

class Solution:
    def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]:
        median_finder = MedianFinder(k)
        left = 0
        for right in range(k):
            median_finder.add_value(nums[right])
        res = [median_finder.find_median()]
        while right < len(nums) - 1:
            right += 1
            median_finder.remove_value(nums[left])
            median_finder.add_value(nums[right])
            left += 1
            print(left, right)
            res.append(median_finder.find_median())
        return res


33521