Stone Weight

ARRAY

### Problem

You are given an array of integers `stones`

where `stones[i]`

represents the weight of the ith stone. Stones are to be combined according to the following rules:

- Pick the two heaviest stones. If their weights are the same, both stones are destroyed. If not, the stone of lesser weight is destroyed, and the stone of greater weight has its weight reduced by the weight of the stone that was destroyed.

Continue this process until there is at most one stone left. Return the weight of this stone (or 0 if there are no stones left).

### Examples

`stoneWeight([15,7,10]) // returns 2 /* We pick 15 and 10. We destroy 10 and transform 15 to 15 - 10 = 5 The new set becomes [7,5]. Next, we pick 7 and 5. The last stone weight is 2. */ stoneWeight([5,5,4,4]) // returns 0 /* We pick both 5 and destroy them Similarly, with 4, they destroy each other leaving no stone behind. */`

Loading...