Data Structures19 sections · 729 units
Open in Course

Problem - Last Stone Weight

Simple heap problem

You have a collection of stones, each with a positive weight. Each turn, you take the two heaviest stones and smash them. If they're equal, both are destroyed.

Otherwise, the lighter one is destroyed and the heavier one loses weight equal to the lighter. Return the weight of the last stone, or 00 if no stones remain.

You're solving a warm-up heap problem. You repeatedly need the two largest elements.