Bootstrap

C语言 | Leetcode C语言题解之第517题超级洗衣机

题目:


题解:

int findMinMoves(int* machines, int machinesSize){
    int sum=0;
    for(int i=0;i<machinesSize;i++){
        sum+=machines[i];
    }
    if(sum%machinesSize!=0){
        return -1;
    }
    int p=sum/machinesSize;
    int ans=0;
    int cur=0;
    for(int i=0;i<machinesSize;i++){
        cur+=(machines[i]-p);
        ans=fmax(ans,machines[i]-p);
        ans=fmax(ans,abs(cur));
    }
    return ans;
}
;