Skip to content

Commit b463e80

Browse files
authored
Create 2155-all-divisions-with-the-highest-score-of-a-binary-array.js
1 parent 66a8fa9 commit b463e80

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var maxScoreIndices = function(nums) {
6+
const n = nums.length
7+
// if(n === 1) return [0]
8+
const leftZero = Array(n).fill(0), rightOne = Array(n).fill(0)
9+
for (let i = 0, sum = 0; i < n; i++) {
10+
if(nums[i] === 0) sum++
11+
leftZero[i] = sum
12+
}
13+
for (let i = n - 1, sum = 0; i >= 0; i--) {
14+
if(nums[i] === 1) sum++
15+
rightOne[i] = sum
16+
}
17+
let hash = {}
18+
for (let i = 0, sum = 0; i <= n; i++) {
19+
20+
hash[i] = (i === 0 ? 0 : leftZero[i - 1]) + (i === n ? 0 : rightOne[i])
21+
}
22+
const max = Math.max(...Object.values(hash))
23+
const res = []
24+
Object.keys(hash).forEach(k => {
25+
if(hash[k] === max) res.push(+k)
26+
})
27+
return res
28+
};

0 commit comments

Comments
 (0)