Last Stone Weight

Easy

You are given an array of integers stones, where stones[i] is the weight of the i-th stone.

The stones are smashed together one turn at a time. On each turn, take the two heaviest stones, with weights x <= y, and smash them:

  • If x == y, both stones are destroyed.
  • If x < y, the lighter stone is destroyed and the heavier one now weighs y - x. It goes back into the pile.

The game ends when at most one stone is left. Return the weight of that last stone, or 0 if no stones are left.

Examples

Example 1

Input:  stones = [2,7,4,1,8,1]
Output: 1
  • Smash 8 and 7. A stone of weight 1 goes back: [2,4,1,1,1]
  • Smash 4 and 2. A stone of weight 2 goes back: [2,1,1,1]
  • Smash 2 and 1. A stone of weight 1 goes back: [1,1,1]
  • Smash 1 and 1. Both are destroyed: [1]

One stone of weight 1 is left.

Example 2

Input:  stones = [9,3,2]
Output: 4

Smashing 9 and 3 leaves a stone of weight 6. That new stone is now the heaviest in the pile, so the next turn smashes 6 and 2, leaving 4. The leftover stone has to go back into the pile, and it can come straight back out as the heaviest.

Example 3

Input:  stones = [3,3]
Output: 0

The two stones weigh the same, so both are destroyed and nothing is left.

Example 4

Input:  stones = [1]
Output: 1

There is only one stone, so there is nothing to smash.

Example 5

Input:  stones = [2,3,6,2,4]
Output: 1
  • Smash 6 and 4. A stone of weight 2 goes back: [2,3,2,2]
  • Smash 3 and 2. A stone of weight 1 goes back: [2,2,1]
  • Smash 2 and 2. Both are destroyed: [1]

Constraints

  • 1 <= stones.length <= 30
  • 1 <= stones[i] <= 1000
TypeScript
Loading editor…
Test cases38
Case 1
stones = [2,7,4,1,8,1]
Case 2
stones = [1]
Case 3
stones = [3,3]
Case 4
stones = [9,3,2]
Case 5
stones = [2,3,6,2,4]
Case 6
stones = [1,2]
Case 7
stones = [2,2]
Case 8
stones = [1,3]
Case 9
stones = [1000]
Case 10
stones = [5,5,5]
Case 11
stones = [5,5,5,5]
Case 12
stones = [10,4,2,10]
Case 13
stones = [7,6,7,6,9]
Case 14
stones = [1,1,2,3,5]
Case 15
stones = [3,7,2]
Case 16
stones = [1,1000]
Case 17
stones = [1000,1]
Case 18
stones = [8,10,4]
Case 19
stones = [1,2,3,4,5,6,7,8,9,10]
Case 20
stones = [10,9,8,7,6,5,4,3,2,1]
Case 21
stones = [100,90,80,70,60,50,40,30,20,10]
Case 22
stones = [31,26,33,21,40]
Case 23
stones = [1,1,1,1,1,1,1]
Case 24
stones = [1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000]
Case 25
stones = [1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000,1000]
Case 26
stones = [2,4,8,16,32,64,128,256,512]
Case 27
stones = [999,1,1,1,1,1,1,1,1,1]
Case 28
stones = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
Case 29
stones = [157,52,828,133,196]
Case 30
stones = [613,193,419,104,233,27,29,505]
Case 31
stones = [432,431,568,843,42,968,81,747,444,239,796,507]
Case 32
stones = [762,922,906,723,213,262,394,833,178,206,497,799,335,418,133,266,378]
Case 33
stones = [912,977,327,90,12,858,824,121,674,220,942,967,273,941,314,578,663,555,642,194,277,86,312]
Case 34
stones = [154,537,770,400,331,600,375,728,600,953,99,394,582,611,723,737,661,394,652,769,335,801,935,363,446,643,295,619,933,147]
Case 35
stones = [39,250,386,897,904,528,824,346,307,816,202,627,854,61,539,650,535,634,64,227,25,485,981,135,822,557,736,131,249,306]
Case 36
stones = [10,4,5,10,4,8]
Case 37
stones = [2,10,9,6,9,6,7,7,7,6,2,7,6,5,8]
Case 38
stones = [9,9,9,1,2,2,9,4,9,8,6,8,1,10,3,5,1,6,3,4,2,9,4,4,4,10,9,8,5,3]