Skip to content
Decoder

Last Stone Weight

Easy

Problem

You have a pile of stones with weights stones. Each turn, take the two heaviest stones, x <= y, and smash them together:

- if x == y, both are destroyed; - otherwise the x stone is destroyed and the y stone now weighs y - x.

Keep going until at most one stone is left. Return its weight, or 0 if none remain.

Example 1

Input
[2, 7, 4, 1, 8, 1]
Output
1
Why
8,7 → 1; 4,2 → 2; 2,1 → 1; 1,1 → 0; one stone of 1 left

Example 2

Input
[2, 2]
Output
0

Example 3

Input
[1]
Output
1

Constraints

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

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.