Skip to content
AI360Xpert
Beta
Difficulty: EasyHeap / Priority Queue

Last Stone Weight

Problem in Plain English

You are given stone weights and repeatedly smash the two heaviest stones, keeping the difference when they differ and destroying both when they match. Return the last remaining weight, or 0 when no stones survive.

Problem Statement

Given an array of integers stones, where each entry is the weight of one stone, smash the stones together until at most one remains.

On each turn, pick the two heaviest stones with weights x and y where x≤yx \le y. When both weights match, the two stones destroy each other; otherwise the lighter stone is destroyed and the heavier stone continues with weight y−xy - x.

Return the weight of the last remaining stone, or 0 when no stones are left.

Constraints

  • 1 <= stones.length <= 30
  • 1 <= stones[i] <= 1000

Examples

Example 1

Input
stones = [2,7,4,1,8,1]
Output
1

Smash 8 and 7 into 1, then 4 and 2 into 2, then 2 and 1 into 1, then the two 1 stones destroy each other, leaving a single 1.

Example 2

Input
stones = [1]
Output
1

There is only one stone, so no smash happens and its weight is the answer.

Example 3

Input
stones = [10,10,3]
Output
3

The two heaviest stones both weigh 10, so both disappear. The remaining 3 survives without another smash. Equal weights are separate stones, so duplicates must not be collapsed.

Example 4

Input
stones = [2,2]
Output
0

The only two stones have equal weights and both disappear. An empty pile returns 0; there is no surviving zero-weight stone.

Intuition

The rule fixes which pair to smash: always choose the two heaviest live stones, including any difference created earlier. In Example 1, 8 and 7 become 1, so the next pair is 4 and 2 rather than the next two original entries. We only need a collection that retains duplicate weights and repeatedly exposes its two largest values. Sorting provides a direct simulation; a max heap maintains just the ordering needed to get each maximum.

Approaches

Repeated Sorting

Solution Details

Reveal Repeated Sorting: intuition, complexity, and code

Hints

Hint 1
Follow the exact simulation: a new difference participates in choosing the next two heaviest stones.
Hint 2
Sorting the current pile on every round exposes the two largest entries, but repeats work.
Hint 3
A max heap returns the heaviest live stone with each pop in O(log⁡n)O(\log n) time. For portable Python code, use a min heap of negative weights.

Edge Cases

  • A single stone such as [1] needs no smash, so the answer is that stone itself.
  • Two equal stones such as [2, 2] destroy each other and leave an empty pile, so return 0 and never index into the empty heap.
  • Equal pairs late in the game, like the three 1 stones in Example 1, can shrink the pile by two at once without pushing anything back.
  • Two unequal stones such as [1,1000] leave 999, which needs no further smash.
  • Thirty identical maximum-weight stones all cancel in pairs; an odd number of identical stones leaves one.

Common Mistakes and Interview Tips

  • Selecting stones from the original sorted order after a smash: the difference must compete with all remaining stones.
  • Treating equal weights as one set entry loses separate stones and breaks cancellation.
  • Using JavaScript's default sort compares strings; use (a, b) => a - b for numeric weights.
  • Mixing negated heap entries and real weights in Python subtraction. Convert both popped values back, then negate a surviving difference on insertion.
  • Reading the top of an empty heap after equal stones destroy each other; guard the final return.
  • Interview tip: inserting a zero for an equal pair models a nonexistent stone. It needlessly retains entries, even though with nonnegative weights the final numeric answer may still be unchanged.

Key Takeaway

Whenever a simulation repeatedly needs the largest or smallest live element, a heap replaces each linear scan or full sort with a logarithmic pop and push.