← All problems

Last Stone Weight

Easy heapssimulation LeetCode ↗
You are given an array of stone weights. Repeatedly take the two heaviest stones: if they are equal, both are destroyed; otherwise the heavier is reduced by the lighter's weight and put back. Continue until at most one stone remains, and return its weight (or 0 if none remain). Simulate this efficiently.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(n log n), space O(n).

Common pitfalls

  • Pushing back a zero difference, causing an infinite loop.
  • Forgetting to negate when using a min-heap as a max-heap.
  • Re-sorting the array each round for O(n^2 log n) instead of using a heap.

Your solution

Log in to save your code and track progress.