Problem context and objectives
Mission briefing
Matchmaking coordinator · Urgent players and jobs compete for limited slots
Keep the best next candidate available without sorting everything again: Summit K.
A stale priority frontier increases wait time for everyone.
How you win
- 1Recognize when Top K heap matches the clues
- 2Keep this true after every move: the heap contains the k largest values seen so far
- 3Reach the result within O(n log k)
Rules and pressure
- Target cost: O(n log k)
- State rule: the heap contains the k largest values seen so far
Lesson 1 of 3
Live algorithm trace
Top K heap
Complete execution1 of 8
3
scan
2
1
5
6
4
Begin with an empty min-heap that may hold two candidates.
1
heap = []2
push each value3
if len(heap) > k: pop minimum4
return heap[0]k = 2heap = []
Truth to preserve / Cost target
Truth to preserve
the heap contains the k largest values seen so far
Cost target
O(n log k)
A min-heap of size k remembers the k largest values seen. Its root is the weakest current candidate and is replaced when a stronger value arrives.
Your call · What should guide every step of this algorithm?