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

1
class Solution {
2
public List<Integer> maxScoreIndices(int[] nums) {
3
int N = nums.length;
4
List<Integer> res = new ArrayList<>();
5

6
int[] pref = new int[N + 1];
7
pref[0] = 0; // at zeroth division we have no elements
8
for (int i = 0; i < N; ++i) pref[i + 1] = nums[i] + pref[i];
9

10
int maxScore = -1;
11
int onesToRight, zeroesToLeft, currScore;
12

13
for (int i = 0; i < N + 1; ++i) {
14
onesToRight = pref[N] - pref[i];
15
zeroesToLeft = i - pref[i];
16
currScore = zeroesToLeft + onesToRight;
17

18
if (currScore > maxScore) {
19
res.clear();
20
maxScore = currScore;
21
}
22
if (currScore == maxScore) res.add(i);
23
}
24
return res;
25
}
26
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0