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;
}
相关推荐
什巳21 小时前
JAVA练习309- 二叉树的层序遍历
java·数据结构·算法·leetcode
麻瓜老宋1 天前
AI开发C语言应用按步走,表达式计算器calc的第一步,中缀表达式词法分析
c语言·开发语言·atomcode
什巳1 天前
JAVA练习306- 翻转二叉树
java·数据结构·算法·leetcode
一个初入编程的小白1 天前
C语言:函数栈帧与销毁
c语言·开发语言
smj2302_796826521 天前
解决leetcode第3989题网格中保持一致的最大列数
python·算法·leetcode
ComputerInBook1 天前
c 和 c++ 中的宏块(macro)
c语言·c++··宏块·宏指令
bu_shuo1 天前
c与cpp中的argc和argv
c语言·c++·算法
Aurorar0rua1 天前
CS50 x 2024 Notes Algorithms - 02
c语言·开发语言·学习方法
CoderYanger1 天前
A.每日一题:3020. 子集中元素的最大数量
java·程序人生·算法·leetcode·面试·职场和发展·学习方法
足球中国1 天前
nuget把缓存搬离C盘
c语言·windows·缓存