Last Stone Weight

Given a multiset of stone weights, repeatedly smash the two heaviest stones together. If the stones have equal weight, both are destroyed; otherwise the difference remains. Return the weight of the last remaining stone, or 0 if none remain.

Input Format

stones = array of stone weights

Output Format

weight of the final stone, or 0

Constraints

  • 1 <= stones.length <= 10^5
  • 1 <= stones[i] <= 10^9

Examples

Example 1:

Input:

stones = [2,7,4,1,8,1]

Output:

1

Explanation:

Repeated smashing leaves a final stone of weight 1.

Example 2:

Input:

stones = [1]

Output:

1

Explanation:

Only one stone exists, so it remains unchanged.

Loading...
Last Stone Weight - Heap DSA Problem