Skip to content

Commit 55f8340

Browse files
committed
New Problem Solution -"Minimum Number of Operations to Move All Balls to Each Box"
1 parent c8e75fd commit 55f8340

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ LeetCode
2222
|1779|[Find Nearest Point That Has the Same X or Y Coordinate](https://leetcode.com/problems/find-nearest-point-that-has-the-same-x-or-y-coordinate/) | [C++](./algorithms/cpp/findNearestPointThatHasTheSameXOrYCoordinate/FindNearestPointThatHasTheSameXOrYCoordinate.cpp)|Easy|
2323
|1774|[Closest Dessert Cost](https://leetcode.com/problems/closest-dessert-cost/) | [C++](./algorithms/cpp/closestDessertCost/ClosestDessertCost.cpp)|Medium|
2424
|1773|[Count Items Matching a Rule](https://leetcode.com/problems/count-items-matching-a-rule/) | [C++](./algorithms/cpp/countItemsMatchingARule/CountItemsMatchingARule.cpp)|Easy|
25+
|1769|[Minimum Number of Operations to Move All Balls to Each Box](https://leetcode.com/problems/minimum-number-of-operations-to-move-all-balls-to-each-box/) | [C++](./algorithms/cpp/minimumNumberOfOperationsToMoveAllBallsToEachBox/MinimumNumberOfOperationsToMoveAllBallsToEachBox.cpp)|Medium|
2526
|1768|[Merge Strings Alternately](https://leetcode.com/problems/merge-strings-alternately/) | [C++](./algorithms/cpp/mergeStringsAlternately/MergeStringsAlternately.cpp)|Easy|
2627
|1763|[Longest Nice Substring](https://leetcode.com/problems/longest-nice-substring/) | [C++](./algorithms/cpp/longestNiceSubstring/LongestNiceSubstring.cpp)|Easy|
2728
|1761|[Minimum Degree of a Connected Trio in a Graph](https://leetcode.com/problems/minimum-degree-of-a-connected-trio-in-a-graph/) | [C++](./algorithms/cpp/minimumDegreeOfAConnectedTrioInAGraph/MinimumDegreeOfAConnectedTrioInAGraph.cpp)|Hard|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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

Comments
 (0)