24 · Heaps & Priority Queues

#1046 Last Stone Weight

Easysolve on LeetCode ↗

frequently reported atNVIDIAPayPalAmazonSalesforceBloombergGoogle

the problem

In plain words

Each turn, smash the two heaviest stones x ≤ y. Equal → both vanish; otherwise a stone of y − x remains. Return the last stone's weight, or 0 if none remain.

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

out 1

8,7→1; 4,2→2; 2,1→1; 1,1→0; last stone 1.

in stones = [1]

out 1

  • 1 ≤ stones.length ≤ 30
  • 1 ≤ stones[i] ≤ 1000

your turn

Try it before you peek

Read the examples, say the brute force out loud, then write code: here or on LeetCode ↗. Stuck for 20 minutes? Open one hint at a time.

Solve it in the browser

LeetCode-style: complete the class. Compiled by the real javac (Java 17) and judged right here — nothing is sent anywhere.

    Solutions are hidden so you can try first. Even 20 minutes of real effort makes the answer stick far better than reading it.

    Marking the problem solved, or opening every hint, also shows it.

    practise next

    Similar problems

    1. #621
      Task Scheduler
      Mediumin Heaps
    2. #502
      IPO
      Hardin Heaps