All Divisions With the Highest Score of a Binary Array LeetCode solution in C++

1
class Solution {
2
public:
3
vector<int> maxScoreIndices(vector<int> &a) {
4
int n = a.size();
5

6
int one = accumulate(a.begin(), a.end(), 0), zero = 0;
7

8
int maxs = 0;
9
vector<int> ans;
10
for (int i = 0; i <= n; i++) {
11
int s = one + zero;
12
if (s > maxs) {
13
maxs = s;
14
ans = {i};
15
} else if (s == maxs) {
16
ans.push_back(i);
17
}
18
if (i < n) {
19
one -= (a[i] == 1);
20
zero += (a[i] == 0);
21
}
22
}
23
return ans;
24
}
25
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0