Last Stone Weight
Last Stone Weight is LeetCode 1046 (Easy). You get an array stones of positive weights. Each turn, take the two heaviest stones, y and x with x <= y, and smash them together:
- If
x == y, both stones are destroyed. - Otherwise
xis destroyed andybecomesy - x.
Repeat until at most one stone is left. Return its weight, or 0 if no stone is left.
- 1 <= stones.length <= 30
- 1 <= stones[i] <= 1000
Intuition
Every turn asks the same thing: which stone is heaviest, and which comes next? Then one new stone may join the pile. That is exactly what a max-heap is built for: it hands back the largest item and accepts a new one, each in O(log n).
- Pop twice: the top two stones are this turn's pair.
- Push the leftover: if
y > x, pushy - x; if they are equal, push nothing. - Stop at one: each turn removes at least one stone, so there are at most
n - 1turns.
The heap's top is always the true heaviest stone, so the loop follows the rules exactly.
"Keep taking the largest item, then maybe add a new one" is the signal for a max-heap. Connecting ropes, running the biggest job first and this smashing game all have that shape.
Approach
Before reading on, pick a structure that hands you the heaviest stone quickly even after new stones are added. Then decide what to push back when two stones are equal. Aim for O(n log n).
Put every stone in a max-heap
Build a max-heap (the priority queue in C++ and Java) from the whole array in one go, which costs O(n). Python's heapq is a min-heap, so store -w for each weight: the most negative number sits on top, and it belongs to the heaviest stone.
Pop the two heaviest stones
While the heap holds two or more stones, pop twice. The first pop is the heavier stone y, so y - x is never negative and needs no abs.
Push back only a real leftover
If y != x, push y - x (as -(y - x) in Python). If they are equal, push nothing, since both stones are gone. A pushed 0 would not change the answer, but it would waste a turn smashing against nothing.
Return what is left
When the loop ends the heap holds one stone or none. Return that stone's weight, turned back to positive in Python, or 0 when every stone was destroyed.
Code in Python, C++ and Java
Common pitfalls
Pushing the leftover without negating it
heapq.heappush(heap, y - x)
heapq.heappush(heap, -(y - x))
Every other value in the heap is negative, so a positive leftover reads as a stone of negative weight. On [2,7,4,1,8,1] the function returns -1 instead of 1.
Using Java's default PriorityQueue
new PriorityQueue<>()
new PriorityQueue<>(Collections.reverseOrder())
The default queue is a min-heap, so poll() hands out the lightest stones and y - x goes negative. On [2,7,4,1,8,1] the answer comes out as -17.
Reading the top of an empty heap
return -heap[0]
return -heap[0] if heap else 0
When the stones cancel out completely, as in [2,2], the heap is empty and heap[0] raises an IndexError. The rules say the answer is 0.
Edge cases
[5]The loop never runs, because the heap starts with one stone. Its weight, 5, is returned as it is.
Complexity
Last Stone Weight FAQ
Can it be solved without a heap?
Yes. With at most 30 stones you can sort the list every turn and smash the last two: O(n² log n), but instant at this size. Since weights are at most 1000, a count array indexed by weight also works, scanning from the heaviest weight down in O(n + 1000). The heap is the version that still scales when the limits grow.
How is it different from Last Stone Weight II?
In Last Stone Weight II (1049) you may smash any two stones, and you want the smallest possible final weight. That turns into splitting the stones into two piles with sums as close as possible, a subset-sum DP. Here the choice is forced (always the two heaviest), so you simply play the game out.