Two heaps
medium 22 minThe two heaps pattern keeps a max-heap for the smaller half of the values and a min-heap for the larger half.
Check yourself
One question at a time. Each answer explains why.
Question 1 of 10
What does the max-heap store in the two heaps pattern?
What is the time complexity of finding the median?
By how much can the heap sizes differ?
If both heaps have the same size, the median is:
Using the route-then-rebalance rule, the stream 41, 35, 62, 4 has been added. What is the median?
low = [41, 4, 35] and high = [62, 97]. The value 50 arrives. What happens?
Which rebalance condition is wrong for the convention where low may hold the extra value?
In the sliding window median, a value leaves the window but is not at either root. What should happen?
IPO with w = 1, k = 1, projects (capital 0, profit 2) and (capital 5, profit 100). What is the final capital?
Why is re-sorting after every value a poor way to answer a running median?
Pick an answer to see why.
Not quite. The max-heap holds the smaller half so that its largest value, the one right below the middle, is on top.
Not quite. The median is read from the two roots, which costs constant time.
Not quite. The balance invariant keeps the sizes equal or one apart, so the middle value or values are always the roots.
Not quite. With an even count the two middle values are max(low) and min(high), so the median is their average.
Not quite. After the four adds low = {35, 4} and high = {41, 62}. Even count: (35 + 41) / 2 = 38.0. Sorted: 4, 35, 41, 62.
Not quite. 50 > 41, so it joins high. Sizes become 3 and 3, which is balanced. Median = (41 + 50) / 2 = 45.5.
Not quite. Moving whenever low is merely bigger leaves high one bigger on every odd count, so low's top is no longer the median.
Not quite. Lazy deletion keeps every operation O(log n). Ignoring it entirely would leave the sizes wrong and let the value resurface later.
Not quite. Only the first project is affordable, so the greedy takes it: 1 + 2 = 3. The 100 project needs capital 5.
Not quite. Re-sorting repeats almost all the work each time. Two heaps keep the split around the middle and pay only O(log n) per value.
Flashcards
Say your answer, then reveal it.
Card 1 of 11
Which heap holds the smaller half, and why that kind?
A max-heap, because you need the largest of the small values on top, right below the middle.
Say your answer, then reveal it.
State the two invariants.
Order: max(low) ≤ min(high). Balance: low has the same size as high or exactly one more.
Say your answer, then reveal it.
How does add decide where a value goes?
If low is empty or the value is at most low's top, push into low; otherwise push into high. Then rebalance.
Say your answer, then reveal it.
Why is one move enough to rebalance?
The gap is 0 or 1 before a push, -1 to 2 after it, and a move changes it by 2, landing back on 0 or 1.
Say your answer, then reveal it.
How do you read the median?
Odd count: low's top. Even count: the average of both tops, with true division.
Say your answer, then reveal it.
Costs of the two heaps running median?
add O(log n), findMedian O(1), space O(n). A whole stream costs Θ(n log n), against Θ(n² log n) for re-sorting each time.
Say your answer, then reveal it.
How do you get a max-heap from Python's heapq?
Push -x and negate again when you read or pop.
Say your answer, then reveal it.
What is lazy deletion in the sliding window median?
Record a leaving value in a gone counter and adjust a live size; pop it only when it reaches a root.
Say your answer, then reveal it.
Why balance on live counters in the sliding window?
len() still counts dead entries waiting to be pruned, so it misstates the real halves.
Say your answer, then reveal it.
What are the two heaps in IPO?
A min-heap by capital for locked projects and a max-heap by profit for projects you can afford.
Say your answer, then reveal it.
How do you track the 90th percentile with two heaps?
Keep the smallest ceil(0.9 · n) values in low; the answer is low's top.
Say your answer, then reveal it.
These cards come back in Today, just before you would forget them.