This question wants us to find all the medians for a list when the window is size k. We then slide the median all the way through the window.
class Solution:
def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]:
window = SortedList(nums[:k])
res = []
def get_median():
if k % 2 == 1: # odd length
return float(window[k // 2])
return (window[k // 2] + window[(k // 2) - 1]) / 2.0
res.append(get_median())
for i in range(k, len(nums)):
window.remove(nums[i - k])
window.add(nums[i])
res.append(get_median())
return res