Tap here to switch tabs
Problem
Submissions

Problem: Last Stone Weight

Last Stone Weight gives an array of stone weights. Repeatedly smash the two heaviest stones, replacing unequal pairs with their difference, and return the final weight or zero if none remain.

Statement

You are given an array stones where each element represents the weight of a stone. Repeatedly perform the following operation while there are at least two stones remaining:

Select the two stones with the largest weights, say x and y with x <= y. Smash them together:
If x == y, both stones are destroyed.
If x != y, the stone of weight x is destroyed and the stone of weight y becomes a new stone with weight y - x.

Return the weight of the final remaining stone. If no stones remain, return 00.

Constraints:

  • 1≤1 \leq stones.length ≤30\leq 30

  • 1≤1 \leq stones[i] ≤1000\leq 1000

⋮
Tap here to switch tabs
Problem
Submissions

Problem: Last Stone Weight

Last Stone Weight gives an array of stone weights. Repeatedly smash the two heaviest stones, replacing unequal pairs with their difference, and return the final weight or zero if none remain.

Statement

You are given an array stones where each element represents the weight of a stone. Repeatedly perform the following operation while there are at least two stones remaining:

Select the two stones with the largest weights, say x and y with x <= y. Smash them together:
If x == y, both stones are destroyed.
If x != y, the stone of weight x is destroyed and the stone of weight y becomes a new stone with weight y - x.

Return the weight of the final remaining stone. If no stones remain, return 00.

Constraints:

  • 1≤1 \leq stones.length ≤30\leq 30

  • 1≤1 \leq stones[i] ≤1000\leq 1000