Skip to content
Onsite

Last Stone Weight

EasyP3Coding

You have a collection of stones with positive integer weights. Repeat this until at most one stone is left: take the two heaviest stones, with weights x <= y, and smash them together. If x == y both stones are destroyed; otherwise the stone of weight x is destroyed and the other becomes weight y - x.

Return the weight of the last remaining stone, or 0 if no stone remains.

Example 1

Input
stones = [2,7,4,1,8,1]
Output
1
Why
8 and 7 give 1; 4 and 2 give 2; 2 and 1 give 1; 1 and 1 are both destroyed; one stone of weight 1 remains.

Example 2

Input
stones = [1]
Output
1

Constraints

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

Hints

Run checks the 3 visible tests, Submit all 10.