All problems
1116MediumArrayStringPrefix Sum

Gathering the Crates Into Every Bay

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1769Minimum Number of Operations to Move All Balls to Each Box

This 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.

Examples

Example 1

Input
bays = "1001"
Output
[3, 3, 3, 3]

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

Input
bays = "111"
Output
[3, 2, 3]

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

Input
bays = "10"
Output
[0, 1]

The crate already sits in the first bay, so nothing moves, and reaching the second bay takes one step.

Constraints

  • 1 <= bays.length <= 2000
  • every character of bays is 0 or 1

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def min_operations(bays: str) -> list[int]:
Java
public int[] minOperations(String bays)
September 7
Apply