You have n boxes represented as a binary string boxes
, where:
'0'
means the box is empty.'1'
means the box contains one ball.You can move a ball from one box to an adjacent box in one operation. A box is adjacent if the difference in their indices is 1.
You need to calculate an array answer
of size n
where:
answer[i]
is the minimum number of operations required to move all balls to the ith box, considering the initial positions of the balls.Input:
boxes = "110"
Output:
[1, 1, 3]
all balls
to the 0th
index box from the initial state = 1
.all balls
to the 1st
index box from the initial state = 1
.all balls
to the 3rd
index box from the initial state = 2
.total number
of operations required to move all balls
to each index box from the initial state
is given by the array:ans = [1, 1, 3]
Explanation:
1 operation
.1 operation
.2 operations
.1 operation
.3 operations
.Input:
boxes = "001011"
Output:
[11, 8, 5, 4, 3, 4]
Loading component...
Loading component...