Last Stone Weight
Given an array stones where each value is a stone's weight, repeatedly take the two heaviest stones x <= y and smash them: if x == y both are destroyed, otherwise the heavier is destroyed and a stone of weight y - x remains. Continue until at most one stone is left; return its weight, or 0 if none remain.
Open official problem prompt ↗Simulate the smashing process by always combining the two currently heaviest stones and report what survives.
A knockout tournament where the two strongest remaining fighters clash each round; if they tie both are out, otherwise the survivor re-enters with strength reduced by the loser's, and you ask who is left standing.
- Input
- stones = [2, 7, 4, 1, 8, 1]
- Output
- 1
- Why
- Smash 8&7 -> 1; 4&2 -> 2; 2&1 -> 1; 1&1 -> 0; the single stone left weighs 1.
1 <= stones.length <= 301 <= stones[i] <= 1000