easy +10 pts

Last Stone Weight

Simulate smashing stones with a max-heap to find the final remaining weight.

You are given a list of stone weights. Each turn, you choose the two heaviest stones and smash them together. If they have equal weight, both are destroyed. If they differ, the heavier stone is destroyed and the lighter stone's weight is reduced by the heavier stone's weight. The result (the absolute difference, or 0 if equal) is put back into the list. Continue until at most one stone remains. Write a function `last_stone_weight(stones: list[int]) -> int` that returns the weight of the last remaining stone, or 0 if no stones remain.

Constraints

1 <= len(stones) <= 10^4 0 <= stones[i] <= 10^9

Example

>>> last_stone_weight([2,7,4,1,8,1])
1
>>> last_stone_weight([1])
1
>>> last_stone_weight([])
0
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Use Python's heapq which implements a min-heap; store negative weights to simulate a max-heap.
While there is more than one stone, pop the two largest (most negative) values and push back their difference if non-zero.
If the list becomes empty, return 0.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.