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

题目:


题解:

cpp 复制代码
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;
}
相关推荐
Pniubi16 分钟前
力扣19 . 删除表的倒数第N个节点(栈)
算法·leetcode·职场和发展
文丑颜不良啊16 小时前
LeetCode2761~2780题目解析
java·leetcode
文丑颜不良啊19 小时前
LeetCode2841~2860题目解析
java·leetcode
文丑颜不良啊20 小时前
LeetCode2921~2940题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2901~2920题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2861~2880题目解析
java·leetcode
another heaven1 天前
【C/C++ 第一次跨越(上):用 C++11/14 停止制造新的旧代码】
c语言·c++·制造
another heaven2 天前
【C/C++ Ranges 是零开销抽象吗?拆开管道、惰性求值和编译期膨胀】
c语言·开发语言·c++
Code_Solitude2 天前
C语言动态内存 + 柔性数组 + 数组名退化
c语言·算法·柔性数组
文丑颜不良啊2 天前
LeetCode2821~2840题目解析
java·leetcode