Skip to main content
←
Architecture Workspace
Arch
OS
Workspaces
/
Search blueprints, commands…
⌘K
Build
Learn
Blueprint
▾
Share
Sign in
Menu ▾
ArchOS Studio
Hydrating 525 executable blueprints & local workspace…
ALGORITHM CANVAS · BINARY MIN-HEAP · TIME O(N log K) · SPACE O(K)
Min-Heap Top-K Selection (Binary Heap)
Solve Timed Cold Drill →
✕ Topology View
Maintain a size-K min-heap. The root holds the smallest of the top-K winners; any larger element evicts the root in O(log K).
HEAP TREE (3 ACTIVE NODES)
5
12
29
◀ Prev
▶ Play
Next ▶
STEP 1/5 · L6
1x
nums
=[12,5,29,7,42,18,31]
k
=3
x
=29
heap
=[5,12,29]
PYTHON 3.12 · INSTANT PREVIEW
▸ Trace Live Python (Pyodide)
1
import heapq
2
3
def top_k_stream(nums, k):
4
heap = []
5
for x in nums:
6
heapq.heappush(heap, x)
7
if len(heap) > k:
8
heapq.heappop(heap)
9
return sorted(heap, reverse=True)
CALL: