forked from liuyubobobo/Play-Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
39 lines (29 loc) · 775 Bytes
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/// Source : https://leetcode.com/problems/super-washing-machines/
/// Author : liuyubobobo
/// Time : 2020-04-07
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
/// Mathematics
/// Time Complexity: O(n)
/// Space Complexity: O(1)
class Solution {
public:
int findMinMoves(vector<int>& machines) {
int sum = accumulate(machines.begin(), machines.end(), 0);
int n = machines.size();
if(sum % n) return -1;
int avg = sum / n;
for(int& e: machines) e -= avg;
int res = *max_element(machines.begin(), machines.end()), cur = 0;
for(int e: machines){
cur += e;
res = max(res, abs(cur));
}
return res;
}
};
int main() {
return 0;
}