Window median with two heaps
Given an integer array nums and a window size k, return the median of each sliding window of size k, doubled and cast to int (i.e. return int(2*median) to avoid float ambiguity). The median of an even-sized window is the average of the two middle elements. Use two heaps with lazy deletion so each step is O(log k). 1 <= k <= len(nums) <= 10^5. Return a list of length len(nums) - k + 1.
Implement
window_median2x(nums: list[int], k: int) → list[int]Examples
in
[[1,3,-1,-3,5,3,6,7],3]out[2,-2,-2,6,10,12]What a strong answer looks like
State your approach and its time/space complexity out loud before you optimize. Handle the edge cases (empty input, duplicates, overflow), and say why you chose this over the brute force. Green tests are the floor, not the grade.
0:00 of about 35 min
solution.py
InputExpectedGot
[[1,3,-1,-3,5,3,6,7],3][2,-2,-2,6,10,12]not run yetsample