Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

stones =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

Always smash the two heaviest stones. Use a max-heap.

Approach

Python has min-heap, so store negatives.

  • Pop two largest.
  • If unequal, push back the difference. Repeat until <= 1 stone remains.

Why it works

The rule requires repeatedly selecting the largest two; a heap supports that efficiently.

Code
Loading...
Complexity
Time: O(n log n)
Space: O(n)
Solution
Updated: 2026-02-23

Idea

Always smash the two heaviest stones. Use a max-heap.

Approach

Python has min-heap, so store negatives.

  • Pop two largest.
  • If unequal, push back the difference. Repeat until <= 1 stone remains.

Why it works

The rule requires repeatedly selecting the largest two; a heap supports that efficiently.

Code
Loading...
Complexity
Time: O(n log n)
Space: O(n)