Trains the technique from
LeetCode 1769Minimum Number of Operations to Move All Balls to Each BoxThis 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 corridor has bays in a row, given by bays, where 1 marks a bay holding a crate and 0 marks an empty bay.
One step shifts a crate to a bay next door. A bay may hold any number of crates at once.
Return a list whose entry i is the fewest steps needed to gather every crate into bay i, each entry worked out on its own as though the corridor had never been touched.
Example 1
Two crates stand three bays apart. Gathering them into any bay of the corridor always comes to three steps, since every step towards one is a step away from the other.
Example 2
For the middle bay the two outer crates move one step each and the crate already there moves none. For either end bay the crates move one and two steps.
Example 3
The crate already sits in the first bay, so nothing moves, and reaching the second bay takes one step.
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 min_operations(bays: str) -> list[int]:public int[] minOperations(String bays)See the step-by-step animation, the intuition, and clean code in every language — free, no credit card.