← All problems

Last Stone Weight

easy

You have stones with weights given by stones. Each turn, take the two heaviest stones and smash them together: if they're equal, both are destroyed; otherwise the lighter is destroyed and the heavier's new weight is the difference. Return the weight of the last remaining stone, or 0 if none remain.

Example

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