Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 610 Bytes

480.md

File metadata and controls

31 lines (22 loc) · 610 Bytes

Sliding Window Median

Description

link


Solution : Sliding Windows

  • See Code

Code

Complexity T : O( nlogk )

class Solution:
    def medianSlidingWindow(self, nums: List[int], k: int) -> List[float]:
        window = []
        res = []
        for i in range(len(nums)):
            bisect.insort(window, nums[i])
            if len(window) > k:
                window.remove(nums[i - k])
            if len(window) == k:
                res.append((window[k//2] + window[~k//2])/2)
        return res