Byte by Byte

leetcode) 1769. Minimum Number of Operations to Move All Balls to Each Box / C++ 본문

개발 로그/알고리즘

leetcode) 1769. Minimum Number of Operations to Move All Balls to Each Box / C++

CyberSoak 2021. 9. 18. 16:31

 

class Solution {
public:
    
    vector<int> minOperations(string boxes) {
        int n = boxes.size();
        vector<int> res(n);
        unordered_set<int> st;
        for (int i = 0; i < n; ++i) {
            if (boxes[i] == '1') st.insert(i);
        }
        for (int i = 0; i < n; ++i) {
            for (auto idx : st) {
                res[i] += abs(idx - i);
            }
        }
        return res;
    }
};