Question
Finding the subarray with the least median given a size K and an array of length N
I have been struggling the past month with this problem that was given to us on our course while I was upsolving it. The task is to find the window of size K with the least median in an array of integers. It is also worth noting that K will always be odd, so we need not be worried about even length sequences.
An example would be: [1,3,3,2,1], K = 3 we have [1,3,3], [3,3,2], [3,2,1] and sorting this gives us [1,3,3], [2,3,3], [1,2,3] and thus our answer is 2.
I have implemented a solution that uses a sliding window technique and then sorting each window but obviously, it was the slowest one but is still correct (was only given partial points). Another solution I will attach here is where I used the bisect module, but I think using the remove method increased the time complexity of my program. I wanted to know if there is any solution to this that may have a time complexity of O(n log k) time.
from bisect import insort, bisect_left
from typing import Sequence
def min_median(s: Sequence[int], m: int) -> int:
n = len(s)
result = 9**30
window = sorted(s[:m])
mid = m // 2
result = window[mid]
for i in range(m, n):
insort(window, s[i])
del window[bisect_left(window, s[i - m])]
result = min(result, window[mid])
return result