20210929 力扣每日一题 517超级洗衣机
class Solution {
public:
int findMinMoves(vector<int>& machines) {
int total = 0;
int maxx = -1;
int n = machines.size();
for (int i = 0; i < n; i++) {
total += machines[i];
maxx = max(machines[i], maxx);
}
if (total % n != 0) return -1;
int target = total / n;
int ans = maxx - target;
return ans;
}
};
10分钟写的,感觉就是错的,果不其然WA,通过测试用例:51 / 120
好歹想对了-1
手算模拟后发现,有的洗衣机可能同时一进一出,到达avg可能需要更多步数。
输入:
[0,0,11,5]
输出:
7
预期结果:
8
看了官方题解,加了个abs出问题。
class Solution {
public:
int findMinMoves(vector<int> &machines) {
int tot = a