Trains the technique from
LeetCode 1140Stone Game IIThis is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.
Same function shape, different story and different numbers.
A belt presents piles trays in a fixed order, piles[0] nearest the loading bay. Tray i holds piles[i] blanks. Two crews, Rhea first and then Tomas, take turns clearing trays off the front of the belt.
An allowance m starts at 1. On a turn the crew takes the first x trays that are still on the belt for any x with 1 <= x <= 2 * m, and x may not exceed the number of trays remaining. Every blank on a taken tray goes to that crew. Once the turn is over the allowance becomes max(m, x), and it carries over to the other crew as well. Play stops when the belt is empty.
Both crews want as many blanks as they can get for themselves. Return the number of blanks Rhea ends up with when neither crew ever plays worse than it has to.
Example 1
The reported figure is the total on Rhea's side of the table at the end of a run in which each crew always chooses the batch size that serves it best.
Example 2
One tray is on the belt and the opening allowance already covers it, so Rhea clears the belt on the first turn.
Example 3
The opening allowance permits up to two trays, so Rhea can clear both and Tomas never gets a turn.
Example 4
Seven trays each hold one blank and the figure returned is Rhea's share of the seven.
The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.
def best_first_haul(trays: list[int]) -> int:public int bestFirstHaul(int[] trays)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.