You have n
boxes. You are given a binary string boxes
of length n
, where boxes[i]
is '0'
if the ith
the 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 the 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]
Approach
C++
#include <bits/stdc++.h>using namespace std;vector<int> minOperations(string boxes){int n = boxes.size();vector<int> res(n, 0);int cnt = 0, x = 0;//move all balls from left to//the ith positionfor (int i = 0; i < n; i++){x += cnt;res[i] += x;cnt += (boxes[i] == '1');}cnt = 0, x = 0;//move all from right to the ith positionfor (int i = n - 1; i >= 0; i--){x += cnt;res[i] += x;cnt += (boxes[i] == '1');}return res;}int main(){string boxes = "110";vector<int> res = minOperations(boxes);cout << "[";for (int i = 0; i < res.size(); i++){cout << res[i];if (i != res.size() - 1)cout << ",";}cout << "]";return 0;}
No comments:
Post a Comment