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

# 480. Sliding Window Median
# Hard

# F(3) => F(4)
# 
#
from sortedcontainers import SortedList

class Solution:
    
    # IDEA: SortedList + Sliding Window
    # Time: O(n log k)
    # Space: O(k)
    # Runtime: 176 ms, faster than 83.82% of Python3 online submissions for Sliding Window Median.
    # Memory Usage: 16.3 MB, less than 26.49% of Python3 online submissions for Sliding Window Median.
    
    def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]:  
        # 1 - init   
        is_odd, mid = k % 2, k // 2
        
        # 2 - setup sortedList (Driver code)
        sortedlist: SortedList[(int, int)] = SortedList(nums[:k])
        ans: List[float] = []
        ans.append(self.get_median(sortedlist, mid, is_odd))
        
        # 3 - process nums for each k-size window
        for i in range(len(nums) - k):
            sortedlist.discard(nums[i])
            sortedlist.add(nums[i+k])
            ans.append(self.get_median(sortedlist, mid, is_odd))
        
        return ans
    
    
    # IDEA: Binary Search + Insort + Sliding Window
    # Time: O(n log k)
    # Space: O(k)
    # Runtime: 122 ms, faster than 98.84% of Python3 online submissions for Sliding Window Median.
    # Memory Usage: 16.3 MB, less than 33.49% of Python3 online submissions for Sliding Window Median.

    def medianSlidingWindow2(self, nums: List[int], k: int) -> List[float]:    
        # 1 - init 
        is_odd, mid = k % 2, k // 2
        window = sorted(nums[:k - 1]) # get first k - 1 elements sorted
        ans = []

        # 2 - Process rest N - k elements
        for i in range(k-1, len(nums)):
            # 2.1 - add a new element to sorted array by Insert Sort with size as k
            bisect.insort(window, nums[i])

            # 2.2 - append median to ans
            ans.append(self.get_median(window, mid, is_odd))

            # 2.3 - remove the element at i-k+1 since it is out of k-size window => keep size of window to be k - 1 for next item to be added
            remove_index = bisect.bisect_left(window, nums[i - k + 1])
            del window[remove_index]
            
        return ans
            
    def get_median(self, window, mid, is_odd) -> float:
        return window[mid] if is_odd else (window[mid] + window[mid - 1]) / 2            
        
        
        
27196