Skip to content
BASA DSA

Last Stone Weight

EasyHeap / Priority Queue

Repeatedly remove the two heaviest stones. Equal weights both disappear; otherwise insert their positive difference. Return the last weight or zero when none remain.

Example 1

Input
stones = [3,7,2,7]
Output
1

Explanation

The two 7s cancel, then 3 and 2 leave weight 1.

Example 2

Input
stones = [5,5]
Output
0

Explanation

Equal weights both disappear, leaving answer zero.

Example 3

Input
stones = [9]
Output
9

Explanation

A single stone needs no smash.

Constraints

  • 1 <= number of stones <= 30
  • Weights are 1..1000

Function contractlastStoneWeightArguments: json
Loading draft…
SolutionRuns on your device
Loading draft…