aboutsummaryrefslogtreecommitdiff
path: root/min_num_ops_ball_box.py
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-01-06 10:43:01 +0100
committerJack Sangdahl <[email protected]>2025-01-06 10:43:01 +0100
commit2e10f9743041778aff34b4e6c84710db2098502a (patch)
treee575679e27726ecb02a0db5a3e95aa685a9acfa6 /min_num_ops_ball_box.py
parentb218fc8b0fcd51654a4b9c92fdf57169f00d6ab3 (diff)
downloadleetcode-2e10f9743041778aff34b4e6c84710db2098502a.tar.gz
leetcode-2e10f9743041778aff34b4e6c84710db2098502a.zip
1769. minimum number of operations to move all balls to each box
Diffstat (limited to 'min_num_ops_ball_box.py')
-rw-r--r--min_num_ops_ball_box.py38
1 files changed, 38 insertions, 0 deletions
diff --git a/min_num_ops_ball_box.py b/min_num_ops_ball_box.py
new file mode 100644
index 0000000..59d5233
--- /dev/null
+++ b/min_num_ops_ball_box.py
@@ -0,0 +1,38 @@
+# 1769. Minimum Number of Operations to Move All Balls to Each Box
+
+"""
+you have 'n' boxes. you are given a binary string 'boxes' of length 'n' where
+'boxes[i]' is '0' if the i'th box is empty, and '1' if it contains one ball.
+in one operation, you can move one ball from a box to an adjacent box. box
+'i' is adjacent to box 'j' if 'abs(i - j) == 1'. note that after doing so,
+there may be more than one ball in some boxes. return an array 'answer' of
+size 'n' where 'answer[i]' is the minimum number of operations needed to move
+all the balls to i'th box.
+"""
+
+
+class Solution(object):
+ def minOperations(self, boxes):
+ """
+ :type boxes: str
+ :rtype: List[int]
+ """
+ l_cnt, l_cost, r_cnt, r_cost, n = 0, 0, 0, 0, len(boxes)
+ ans = [0] * n
+ for i in range(n):
+ if boxes[i - 1] == "1":
+ l_cnt += 1
+ l_cost += l_cnt
+ ans[i] = l_cost
+ for i in range(n - 2, -1, -1):
+ if boxes[i + 1] == "1":
+ r_cnt += 1
+ r_cost += r_cnt
+ ans[i] += r_cost
+ return ans
+
+
+if __name__ == "__main__":
+ obj = Solution()
+ print(obj.minOperations(boxes="110"))
+ print(obj.minOperations(boxes="001011"))