517 Super Washing Machines 超级洗衣机

详见:https://leetcode.com/problems/super-washing-machines/description/

C++:

class Solution {
public:
    int findMinMoves(vector<int>& machines)
    {
        int sum = accumulate(machines.begin(), machines.end(), 0);
        if (sum % machines.size() != 0)
        {
            return -1;
        }
        int res = 0, cnt = 0, avg = sum / machines.size();
        for (int m : machines)
        {
            cnt += m - avg;
            res = max(res, max(abs(cnt), m - avg));
        }
        return res;
    }
};

 参考:http://www.cnblogs.com/grandyang/p/6648557.html

posted on 2018-04-22 16:48  lina2014  阅读(196)  评论(0编辑  收藏  举报

导航