1769.cpp (457B)
1 class Solution { 2 public: 3 vector<int> minOperations(const string &boxes) { 4 vector<int> res(boxes.size(), 0); 5 6 for (int i = 0, ops = 0, cnt = 0; i < boxes.size(); i++) { 7 res[i] += ops; 8 ops += cnt += (boxes[i] == '1'); 9 } 10 11 for (int i = boxes.size() - 1, ops = 0, cnt = 0; i >= 0; i--) { 12 res[i] += ops; 13 ops += cnt += (boxes[i] == '1'); 14 } 15 16 return res; 17 } 18 };