End of Year Sale: Use Coupon Code END2025 to Get Extra 25% Off.
03DAYS
:
14HOURS
:
37MINUTES
:
28SECONDS
Leetcode Problem 480. Sliding Window Median
480. Sliding Window Median
Leetcode Solutions
Approach: Two Heaps (Lazy Removal)
Initialize two heaps: a max-heap lo and a min-heap hi.
Initialize a hash table to keep track of invalid elements.
For each window:
a. Insert the incoming element into the appropriate heap.
b. Remove the outgoing element from the heap by marking it in the hash table.
c. Balance the heaps by moving elements between them if necessary.
d. Remove the top elements of the heaps if they are marked as invalid in the hash table.
e. Calculate the median based on the tops of the heaps.