algorythms
All Patterns
Pattern 9

Two Heaps

Two Heaps

Maintain a max-heap for the lower half and a min-heap for the upper half of a stream. The median is always at their tops.

Time

O(log n) per insertion

Space

O(n)

Recognize it when

  • Find median from data stream
  • Problems requiring dynamic "middle" element
  • Scheduling with two priority groups
Progress0/3
0 solved0 attempted