We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
An easy heap / priority queue problem, graded against 6 test cases (3 of them hidden).
A priority queue for "top k" and streaming-median questions.
Reach for it when you see: "Top/smallest k", a running median, or repeatedly needing the current minimum.
More Heap / Priority Queueproblems →Put all stones in a max-heap. Repeatedly pop the two heaviest, and if they differ, push back the difference. When at most one stone remains, that's the answer (0 if none).
The reference solution hand-rolls a small array-based binary max-heap with `push` (sift up) and `pop` (sift down), so it needs no imports or libraries in either language.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.