diff --git a/medium/minimum_number_of_operations_to_move_all_balls_to_each_box.py b/medium/minimum_number_of_operations_to_move_all_balls_to_each_box.py new file mode 100644 index 0000000..2c036e2 --- /dev/null +++ b/medium/minimum_number_of_operations_to_move_all_balls_to_each_box.py @@ -0,0 +1,21 @@ +# https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box + +from typing import List + +class Solution: + def minOperations(self, boxes: str) -> List[int]: + l = len(boxes) + result = [0] * (l + 1) + left, right = 0, 0 + for i in range(l): + if boxes[i] == "1": + result[0] += i + 1 + right += 1 + for i in range(l): + result[i + 1] = result[i] - right + left + if boxes[i] == "1": + right -= 1 + left += 1 + + return result[1:] +