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;
}
相关推荐
·present·6 分钟前
CSAPP(lecture 13_02 Linking)
c语言·c++
文丑颜不良啊2 小时前
LeetCode2241~2260题目解析
java·leetcode
文丑颜不良啊2 小时前
LeetCode2321~2340题目解析
java·leetcode
wuyk55513 小时前
Keil工具链-第05讲-启动文件与__main到底做了什么
c语言·stm32·单片机·嵌入式硬件·物联网
文丑颜不良啊21 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z21 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
冻柠檬飞冰走茶1 天前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊1 天前
LeetCode2161~2180题目解析
java·leetcode
文丑颜不良啊1 天前
LeetCode2141~2160题目解析
java·leetcode
Hhy_11071 天前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio