Code
The highlighted line runs next. Sift-up and sift-down steps happen inside the library's push and pop, so they stay on the push and pop lines.
Max-heap (heap)
tree view every parent ≥ its children, so the heaviest stone is always on top
array view how the heap is really stored: children of index i are 2i+1 and 2i+2
Smash
Why a heap? Re-sorting after every smash would cost O(n log n) per turn. The heap hands over the heaviest stone in O(log n), so the whole game is O(n log n).
Dry-run trace
Every step as a row. Click a row to jump there.
| # | Action | Round | Detail | heap (array) | size |
|---|