Last Stone Standing: Repeated Smash of the Two Heaviest Weights
Heaps, Sorting & SearchAuto-graded · 28 test casesLast updated
Simulate repeatedly removing the two largest values and reinserting their difference, using a max-heap to find the last remaining value or 0.
Last Stone Standing
You are given a list of positive integers weights. Each integer is the weight of one stone in a pile. You will repeatedly smash stones together until the pile is nearly empty.
One round works like this:
- Choose the two heaviest stones currently in the pile. Call their weights
aandb, witha <= b. If several stones share a weight, any of them may be used, since equal weights are interchangeable. - Smash them together:
- If
a == b, both stones are destroyed. - If
a < b, the lighter stone is destroyed and the heavier stone is replaced by a stone of weightb - a, which goes back into the pile.
- If
Keep playing rounds while the pile holds two or more stones.
Task: Return the weight of the last remaining stone. If no stones remain, return 0.
Function
Implement last_stone_weight(weights), which takes the list weights and returns a single integer.
Constraints
1 <= len(weights) <= 10^51 <= weights[i] <= 10^9- A stone of weight
0is never placed back in the pile; equal stones simply vanish. - A pile with a single stone returns that stone's weight.
- Your function should not depend on the input list being sorted.
Examples
Example 1
Input: last_stone_weight([2, 7, 4, 1, 8, 1])
Output: 1
Example 2
Input: last_stone_weight([5, 5])
Output: 0
Example 3
Input: last_stone_weight([1])
Output: 1
Example 4
Input: last_stone_weight([3, 7, 3, 7, 9])
Output: 1
Hints
Hint 1
After each smash, the new stone can land anywhere in the ordering, so sorting once at the start is not enough. You need a structure that repeatedly gives you the current maximum and accepts new insertions.
Hint 2
Python's heapq is a min-heap. Store negated weights so that the smallest stored value corresponds to the heaviest stone.
Hint 3
Loop while the heap has at least two items: pop twice, and push the difference only if it is nonzero. At the end, return the single remaining weight, or 0 if the heap is empty.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each round needs the two largest current values, and may insert one new value (the difference). That pattern, repeated "take the max" plus "insert", is exactly what a max-heap supports efficiently.
Python's heapq only provides a min-heap, so we store negated weights. The smallest stored number then corresponds to the largest real weight.
Steps:
- Negate every weight and call
heapify(linear time). - While the heap has at least two elements:
- Pop the heaviest stone
b, then the next heaviesta, soa <= b. - If
a != b, pushb - a(negated) back into the heap. - If
a == b, push nothing, since both stones are destroyed.
- Pop the heaviest stone
- If the heap has one element, return its weight; if it is empty, return
0.
Why it is correct
- The heap always returns the true current maximum, so each loop iteration performs exactly the round described in the statement.
- When several stones share a weight, which one is picked does not matter. They are interchangeable, so the resulting multiset of weights is the same, and so is the final answer.
- Every round removes at least one stone from the pile (two if equal, otherwise the pile shrinks by one), so the loop terminates after at most
n - 1rounds. - A difference of
0is never pushed, which matches the rule that equal stones simply vanish.
Complexity
- Time: O(n log n).
heapifyis O(n), and there are at mostn - 1rounds, each with two pops and at most one push at O(log n) each. - Space: O(n) for the negated heap copy (the input list is not mutated).
Reference solution
import heapq
from typing import List
def last_stone_weight(weights: List[int]) -> int:
heap = [-w for w in weights]
heapq.heapify(heap)
while len(heap) > 1:
b = -heapq.heappop(heap)
a = -heapq.heappop(heap)
if b != a:
heapq.heappush(heap, -(b - a))
return -heap[0] if heap else 0