← Back to problems LC 295 →

Find Median from Data Stream

LeetCode 295 • Hard • AeroSpace • Anduril • two heaps

Sensor readings arrive online. Maintain running median with a max-heap (low half) + min-heap (high half). addNum O(log n), findMedian O(1).

addO(log n)
medianO(1)
median
lo (max-heap)
hi (min-heap)
Incoming
op
lo[]
hi[]
Ready
Press Play. Balance two heaps so lo holds the lower half.