My clean and concise c++ solution by checking balance and maximum


  • 1
    X
    class Solution {
    public:
        int findMinMoves(vector<int>& machines) 
        {
            int sum=0,maxNum=0;
            for(int val:machines)
                sum+=val,maxNum=max(maxNum,val);
            if(!machines.size()||sum%machines.size()!=0)
                return -1;
            int ave=sum/machines.size(),res=maxNum-ave,balance=0;
            for(int val:machines)
            {
                balance+=val-ave;
                res=max(res,abs(balance));
            }
            return res;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.