shlogg · Early preview
Prashant Mishra @prashantrmishra

Minimizing Operations On Boxes With Balls

Min Operations: Given a string of 1s and 0s, find the minimum number of operations to move all 1s to one side. TC: O(n), SC: O(n)

Problem
This is similar to Product of array except self
TC: O(n) ,SC:O(n)

# this is same as product of subarray except self
class Solution:
    def minOperations(self, boxes: str) -> List[int]:
        res = [0]*len(boxes)
        # intialize balls and moves going from left to right
        balls,moves = 0,0
        for i in range(len(boxes)):
            res[i] = balls+moves
            moves = balls+moves # moves should be updated before the no. of balls are updated, because this tell us the no of moves it took to mvoe all the ball to the current index
            balls+= int(boxes[i]) # on...