Remove Stones to Minimize the Total
Real-world version: Compactor Passes at the Scrap YardThe statement here is our own: the same inputs, outputs and technique.
A scrap yard weighs the shredded metal sitting in each of its bins: bins[i] is the load of bin i in kilos. A booked slot on the yard's compactor gives the crew exactly passes passes, and every one of them has to be used.
A single pass is aimed at one bin of the crew's choosing. It takes away half of that bin's current load, rounded down, and leaves the rest in the bin. A bin holding one kilo therefore loses nothing when a pass is aimed at it, and the same bin may be chosen on as many passes as the crew likes.
Return the smallest total load, across all bins, that can be left in the yard once the booked passes have all been used.
Examples
Example 1
- Input
- bins = [12, 5], passes = 3
- Output
- 6
The loads can be walked down to 3 and 3 by aiming two passes at the first bin and one at the second, leaving 6 kilos in the yard.
Example 2
- Input
- bins = [7, 7, 7], passes = 2
- Output
- 15
Two of the bins drop from 7 to 4 kilos and the third is untouched, so 15 kilos remain.
Example 3
- Input
- bins = [26, 4, 18, 11], passes = 5
- Output
- 22
One workable slot ends with loads 7, 4, 5 and 6, which add up to the figure returned.
Constraints
- 1 <= bins.length <= 10^5
- 1 <= bins[i] <= 10^4
- 1 <= passes <= 10^5
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def least_scrap_left(bins: list[int], passes: int) -> int:public int leastScrapLeft(int[] bins, int passes)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 8 test cases: 8 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's
More on the same topics
- Make Array Zero by Subtracting Equal AmountsReal-world version: Rounds Needed to Clear Every ReadingEasy
- Meeting Rooms IIReal-world version: Peak Charging Bay DemandMedium
- Task SchedulerReal-world version: Press Die CooldownMedium
- Maximum Number of Events That Can Be AttendedReal-world version: Most Inspections BookedMedium