Ctrl K

Boulder Crusher

Easy
A crusher at a building site has a hopper full of boulders; the i-th one weighs weights[i]. On each cycle the crusher picks the two **heaviest** boulders and smashes them together. Say their weights are a ≤ b: - if a == b, both boulders turn to dust; - otherwise the lighter one turns to dust and the heavier one now weighs b - a. Cycles repeat while more than one boulder is left in the hopper. Return the weight of the remaining boulder, or 0 if none is left.
Example 1
weights=[6, 3, 9, 2] → 2

9 and 6 leave 3, so the pile is [3, 3, 2]; the two 3s destroy each other, leaving [2].

Example 2
weights=[5, 5] → 0

Two equal boulders destroy each other, nothing is left, so the answer is 0.

Example 3
weights=[14] → 14
Constraints
  • 1 ≤ len(weights) ≤ 30
  • 1 ≤ weights[i] ≤ 1000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMedals and Places EasyAverage of the Best Five EasyThe Dimmest Rows EasyTwo Picks, Minus One EasyBest Picks in Order Easy
loading Python…
Input
weights=[6, 3, 9, 2]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 108 tests on the server, hidden ones included.