Asked at
Last Stone Weight
EasyVerifiedHeap~15 min
Given an array stones where stones[i] is the weight of a stone, repeatedly smash the two heaviest stones together.
If they are equal, both are destroyed; otherwise the heavier survives with the weight difference. Return the weight of the last remaining stone, or 0 if none remain.
The input arrives as a single array stones (number[]).
Examples
in[2, 7, 4, 1, 8, 1]
out1
Smash 8 and 7 → 1; then 4 and 2 → 2; then 2, 1, 1, 1 → final stone 1.
in[1]
out1
A single stone is never smashed.
Constraints
- 1 ≤ stones.length ≤ 30
- 1 ≤ stones[i] ≤ 1000
- Target: O(n log n) time with a max-heap.
Get help
🔑
Sign in to solve
Sign in to write, run, and submit your solution — and to pick up where your iOS flow left off.