|
| 1 | +// Source : https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/ |
| 2 | +// Author : Hao Chen |
| 3 | +// Date : 2021-03-20 |
| 4 | + |
| 5 | +/***************************************************************************************************** |
| 6 | + * |
| 7 | + * You have n boxes. You are given a binary string boxes of length n, where boxes[i] is '0' if the ith |
| 8 | + * box is empty, and '1' if it contains one ball. |
| 9 | + * |
| 10 | + * In one operation, you can move one ball from a box to an adjacent box. Box i is adjacent to box j |
| 11 | + * if abs(i - j) == 1. Note that after doing so, there may be more than one ball in some boxes. |
| 12 | + * |
| 13 | + * Return an array answer of size n, where answer[i] is the minimum number of operations needed to |
| 14 | + * move all the balls to the ith box. |
| 15 | + * |
| 16 | + * Each answer[i] is calculated considering the initial state of the boxes. |
| 17 | + * |
| 18 | + * Example 1: |
| 19 | + * |
| 20 | + * Input: boxes = "110" |
| 21 | + * Output: [1,1,3] |
| 22 | + * Explanation: The answer for each box is as follows: |
| 23 | + * 1) First box: you will have to move one ball from the second box to the first box in one operation. |
| 24 | + * 2) Second box: you will have to move one ball from the first box to the second box in one operation. |
| 25 | + * 3) Third box: you will have to move one ball from the first box to the third box in two operations, |
| 26 | + * and move one ball from the second box to the third box in one operation. |
| 27 | + * |
| 28 | + * Example 2: |
| 29 | + * |
| 30 | + * Input: boxes = "001011" |
| 31 | + * Output: [11,8,5,4,3,4] |
| 32 | + * |
| 33 | + * Constraints: |
| 34 | + * |
| 35 | + * n == boxes.length |
| 36 | + * 1 <= n <= 2000 |
| 37 | + * boxes[i] is either '0' or '1'. |
| 38 | + ******************************************************************************************************/ |
| 39 | + |
| 40 | +class Solution { |
| 41 | + |
| 42 | +public: |
| 43 | + vector<int> minOperations(string boxes) { |
| 44 | + vector<int> result(boxes.size()); |
| 45 | + //minOperations01(boxes, result); //128ms |
| 46 | + minOperations02(boxes, result); //4s |
| 47 | + return result; |
| 48 | + } |
| 49 | + |
| 50 | + void minOperations01(string& boxes, vector<int>& result ) { |
| 51 | + vector<int> balls; |
| 52 | + for(int i=0; i<boxes.size(); i++) { |
| 53 | + if(boxes[i] == '1') balls.push_back(i); |
| 54 | + } |
| 55 | + |
| 56 | + for (int i=0; i<boxes.size(); i++) { |
| 57 | + int steps = 0; |
| 58 | + for (int j=0; j<balls.size(); j++) { |
| 59 | + steps += abs(balls[j] - i); |
| 60 | + } |
| 61 | + result[i] = steps; |
| 62 | + } |
| 63 | + } |
| 64 | + void minOperations02(string& boxes, vector<int>& result ) { |
| 65 | + //from left to right |
| 66 | + for(int i=0, ops=0, balls=0; i< boxes.size(); i++) { |
| 67 | + result[i] += ops; |
| 68 | + balls += (boxes[i] == '1' ? 1 : 0); |
| 69 | + ops += balls; |
| 70 | + } |
| 71 | + //from right to left |
| 72 | + for(int i=boxes.size()-1, ops=0, balls=0; i>=0; i--) { |
| 73 | + result[i] += ops; |
| 74 | + balls += (boxes[i] == '1' ? 1 : 0); |
| 75 | + ops += balls; |
| 76 | + } |
| 77 | + } |
| 78 | +}; |
0 commit comments