Skip to content

Commit bd0c802

Browse files
authored
Create all-divisions-with-the-highest-score-of-a-binary-array.cpp
1 parent a255691 commit bd0c802

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Time: O(n)
2+
// Space: O(1)
3+
4+
// prefix sum
5+
class Solution {
6+
public:
7+
vector<int> maxScoreIndices(vector<int>& nums) {
8+
const int total = accumulate(cbegin(nums), cend(nums), 0);
9+
vector<int> result;
10+
for (int i = 0, zeros = 0, mx = 0; i <= size(nums); ++i) {
11+
zeros += static_cast<int>(((i - 1 >= 0) ? nums[i - 1] : 0) == 0);
12+
if (zeros + (total - (i - zeros)) > mx) {
13+
mx = zeros + (total - (i - zeros));
14+
result.clear();
15+
}
16+
if (zeros + (total - (i - zeros)) == mx) {
17+
result.emplace_back(i);
18+
}
19+
}
20+
return result;
21+
}
22+
};

0 commit comments

Comments
 (0)