← Heap / Priority Queue

Pattern Recognition Drill

#58 — Heapify an array

Easy Heaps

The Problem

Convert an arbitrary array into a min-heap in-place.

What approach would you use?

Think about it before scrolling down.

Key Signals

Heap / Priority Queue

heapq.heapify does bottom-up sift-down. O(n) because most nodes are near the bottom.

Common Trap

Inserting elements one by one is O(n log n). heapify is O(n) — know the difference.

← #57 #59 →