left_operations
and right_operations
to 0, and left_count
and right_count
to 0. These will keep track of the cumulative operations and the number of balls to the left and right of the current index, respectively.answer
of the same length as boxes
to store the result.boxes
from left to right. For each box, add left_operations
to answer[i]
, then update left_count
and left_operations
based on whether the current box contains a ball.right_operations
and right_count
to 0.boxes
from right to left. For each box, add right_operations
to answer[i]
, then update right_count
and right_operations
based on whether the current box contains a ball.answer
array.