Minimum Number of Operations to Move All Balls to Each Box LeetCode Solution


Frequently asked in Amazon GoogleViews 1591

Problem Statement:

Minimum Number of Operations to Move All Balls to Each Box LeetCode Solution:

You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if the ith 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 the ith box.

Each answer[i] is calculated considering the initial state of the boxes.

Example 1:

Input:

 boxes = "110"

Output:

 [1,1,3]

Explanation:

 The answer for each box is as follows:
1) First box: you will have to move one ball from the second box to the first box in one operation.
2) Second box: you will have to move one ball from the first box to the second box in one operation.
3) Third box: you will have to move one ball from the first box to the third box in two operations, and move one ball from the second box to the third box in one operation.

Example 2:

Input:

 boxes = "001011"

Output:

 [11,8,5,4,3,4]

Constraints:

  • n == boxes.length
  • 1 <= n <= 2000
  • boxes[i] is either '0' or '1'.

Approach:

1. The brute force solution is Simple to do a double iteration and for each ball found, compute the distance and add it to the cost.

2. Now look into the optimized solution.

3. Let pref[i] is the total distance after moving all the ball from the left side to ith box.

4. Let the sum is the number of balls after moving all the balls from the left side to ith box so far.

5. To calculate pref[i]  , we need to move some balls from (i-1)th box by one, so total distance is pref[i]=pref[i-1]+sum.

6. Do the same with suff[i] on the right side,so total distance is suff[i]=suff[i+1]+sum.

7. To calculate the total distance after moving all the balls from the left and the right side to the ith box: ans[i]=pref[i]+suff[i].

Code:

Minimum Number of Operations to Move All Balls to Each Box C++ code:

class Solution {
public:
    #define ll int
    vector<int> minOperations(string boxes) {
        ll n=boxes.size();
        vector<ll>pref(n+1,0),suff(n+1,0);
        ll i,sum=0,prev=0;
        for(i=0;i<n;i++)
        { 
            pref[i]=sum+prev;
            if(boxes[i]=='1')
                sum++;
            prev=pref[i];
           
        }
        sum=0,prev=0;
        for(i=n-1;i>=0;i--)
        { 
            suff[i]=sum+prev;
            if(boxes[i]=='1')
                sum++;
            prev=suff[i];
            
        }
        
        vector<ll>ans(n,0);
        for(i=0;i<n;i++)
            ans[i]=pref[i]+suff[i];
        return ans;
    }
};

Minimum Number of Operations to Move All Balls to Each Box Java code:

class Solution {
    public int[] minOperations(String boxes) {
        int n=boxes.length();
        int i;
        int[] ans=new int[n];
        int[] pref=new int[n];
        int[] suff=new int[n];
        for(i=0;i<n;i++)
        {
            pref[i]=0;
            suff[i]=0;
        }
        
         int sum=0,prev=0;
        for(i=0;i<n;i++)
        { 
            pref[i]=sum+prev;
            if(boxes.charAt(i)=='1')
                sum++;
            prev=pref[i];
           
        }
        sum=0;prev=0;
        for(i=n-1;i>=0;i--)
        { 
            suff[i]=sum+prev;
            if(boxes.charAt(i)=='1')
                sum++;
            prev=suff[i];
            
        }
        
        
        for(i=0;i<n;i++)
            ans[i]=pref[i]+suff[i];
        return ans;
        
    }
}

Complexity Analysis of Minimum Number of Operations to Move All Balls to Each Box Leetcode Solution:

Time Complexity:

Time Complexity is O(N). As we are traversing the whole array to calculate pref[i] and suff[i].

Space Complexity:

Space complexity is O(N). We are taking extra space to store the value.

Translate »