Loading
Loading
A scrapyard compactor takes the two heaviest blocks in the yard and presses them together. If the blocks weigh the same, both are destroyed. Otherwise the lighter block is destroyed and the heavier one loses that much weight, then goes back into the yard.
Given the block weights, run the compactor until at most one block remains. Return that block's weight, or 0 if none is left.
weights = [6, 9, 3, 2, 5][6, 9, 3, 2, 5]press 9 and 6 → a 3 goes back[3, 2, 5, 3]press 5 and 3 → a 2 goes back[3, 2, 2]press 3 and 2 → a 1 goes back[2, 1]press 2 and 1 → a 1 goes back[1]one block left, weight 11Sign in to keep a note here. It is free, and nobody else can see it.
Sign inExample 1
Sign in to open hints.
Struggling is where the learning happens. Open this once you have genuinely tried, or once the hints have run out.
O(n log n) time · O(n) space
Input. Run your code to watch it work on this input, one line at a time.
Running code is turned off on this deployment. You can still read the problem, write code, use hints and the visualizer.
Tab moves focus out of the editor. Press Ctrl+M to let Tab indent instead.
No results yet
Run your code against the sample tests, or submit to run the full suite.
9 and 6 leave 3; 5 and 3 leave 2; 3 and 2 leave 1; 2 and 1 leave 1.
Example 2
Equal weights destroy each other, so nothing is left.