From de3aca0d83fdf4789be6ff1148ba9e03ce395836 Mon Sep 17 00:00:00 2001 From: bumpsoo Date: Mon, 6 Jan 2025 22:20:23 +0900 Subject: [PATCH] https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box --- ...perations_to_move_all_balls_to_each_box.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 medium/minimum_number_of_operations_to_move_all_balls_to_each_box.py 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:] +