You have a collection of stones with given weights. Repeatedly smash together the two heaviest stones: if they're equal, both are destroyed; otherwise the lighter one is destroyed and the heavier one's weight is reduced by the lighter's weight. Return the weight of the last remaining stone, or 0 if none remain.
stones = [2,7,4,1,8,1]1Solve it in the editor. Sign in free to run your Python or JavaScript against test cases, get a verdict, and track your attempts.
Solve on FeatCode →A heap keeps the minimum (or maximum) element accessible in O(1), with O(log n) insert and remove. It's the tool whenever you repeatedly need "the smallest/largest remaining item" without needing everything fully sorted.
Read the full Heap / Priority Queue guide →
Original problem on LeetCode ↗