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;
}
相关推荐
文丑颜不良啊7 小时前
LeetCode2221~2240题目解析
java·leetcode
Navigator_Z8 小时前
LeetCode //C - 1317. Convert Integer to the Sum of Two No-Zero Integers
c语言·算法·leetcode
冻柠檬飞冰走茶9 小时前
字母移动游戏题解:贪心求最长可保留连续子串(附 C 实现)
c语言·开发语言·数据结构·算法·动态规划
文丑颜不良啊9 小时前
LeetCode2161~2180题目解析
java·leetcode
文丑颜不良啊11 小时前
LeetCode2141~2160题目解析
java·leetcode
Hhy_110713 小时前
《C++深度解构06》手撕 string —— 从底层内存管理到深浅拷贝彻底吃透
c语言·开发语言·c++·学习·visual studio
曦樂~1 天前
Cpp面筋
c语言·c++
Dream it possible!1 天前
LeetCode 面试经典 150_多维动态规划_交错字符串(97_C++_中等)
算法·leetcode·面试·动态规划
心抵鹊1 天前
算法之双指针系列(一)
算法·leetcode
Pniubi1 天前
力扣560.和为k的子数组
算法·leetcode·职场和发展