The morning batches
- O(2ⁿ) · Legendary
- Full plan
- Python
- JavaScript
- recursion
- lists
- dictionaries
Problem
Amparo lights the bakery oven before dawn and already has the line of batches ready, each one with its kilos of dough. The morning is divided into shifts, and each shift takes batches that come one after another in the line: the line is not rearranged and no shift is left empty. The heaviest shift is the one that keeps her baking the longest, so she wants to divide the line so that that shift, the worst one, weighs as little as possible.
Write a function that takes batches, a list of positive whole numbers with the kilos of each batch in the order of the line (it comes with at least one), and shifts, a whole number from 1 to the number of batches. Return a whole number: the kilos of the heaviest shift of the best split. With batches of 7, 2, 5, 10 and 8 kilos in two shifts, the answer is 18: the first shift takes 7, 2 and 5, which is 14 kilos, and the second one takes 10 and 8, which is 18. If the cut came after the 10, the shifts would weigh 24 and 8, and the worst one would be 24.
With a single shift the whole line goes in, so the answer is the sum of everything. With one shift per batch, the answer is the heaviest batch. There are mornings with up to 34 batches in 17 shifts.
Examples
The example
[7, 2, 5, 10, 8], 2 → 18
A single shift
[7, 2, 5, 10, 8], 1 → 32
One shift per batch
[7, 2, 5, 10, 8], 5 → 10
Cutting the line in half is not enough
[1, 1, 1, 1, 20], 2 → 20
Besides these, the challenge has hidden tests that are revealed when you submit your solution.
You start with this
Python
def heaviest(batches, shifts):
passJavaScript
function heaviest(batches, shifts) {
}It opens in your browser, with the editor and the tests. This challenge is part of the full plan; the O(1) and O(log n) ones are free.
More O(2ⁿ) challenges
- The necklace both threads sharerecursion · strings · indexes
- The Pompeii graffitorecursion · strings · indexes
- The rowers of Puntrecursion · lists · comparisons
- The satellite's counterweightsrecursion · lists · operations
- The subway routesrecursion · lists · loops
- The two-pan balancerecursion · lists · operations