Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Method:-1
- class Solution {
- public:
- vector<int> minOperations(string boxes) {
- vector <int> left,right;
- int prev_val=0,prev_count=0;
- for(int i=0;i<boxes.size();i++){
- left.push_back(prev_val+prev_count);
- prev_val=prev_val+prev_count;
- if(boxes[i]=='1') prev_count++;
- }
- prev_val=0;
- prev_count=0;
- for(int i=boxes.size()-1;i>=0;i--){
- left[i]+=prev_val+prev_count;
- prev_val=prev_val+prev_count;
- if(boxes[i]=='1') prev_count++;
- }
- return left;
- }
- };
- */
- /*
- Leetcode
- Assume at ith position,We need n opeation to shift all balls into ith box.
- Let's assume we know all the balls in the left and right of ith position
- as per the initial state.
- so,in case of (i+1)th box,all the balls in the left of (i+1)th box will
- move one more box than the ith box and all the balls in the right of (i+1)th
- including (i+1)th box will move one less boxes.
- so,
- arr[i+1]=arr[i]+left-right(including current box)
- */
- class Solution {
- public:
- vector<int> minOperations(string boxes) {
- vector <int> ans;
- int cur=0,right=0,left=0;
- for(int i=1;i<boxes.size();i++){
- if(boxes[i]=='1'){ cur+=i; right++;}
- }
- if(boxes[0]=='1') left++;
- ans.push_back(cur);
- for(int i=1;i<boxes.size();i++){
- ans.push_back(ans.back()-right+left);
- if(boxes[i]=='1'){ left++; right--;}
- }
- return ans;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement