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

1
# Runtime: 5049 ms (Top 67.40%) | Memory: 29.4 MB (Top 23.35%)
2
class Solution:
3
def maxScoreIndices(self, nums: List[int]) -> List[int]:
4
zeroFromLeft = [0] * (len(nums) + 1)
5
oneFromRight = [0] * (len(nums) + 1)
6
for i in range(len(nums)):
7
if nums[i] == 0:
8
zeroFromLeft[i + 1] = zeroFromLeft[i] + 1
9
else:
10
zeroFromLeft[i + 1] = zeroFromLeft[i]
11

12
for i in range(len(nums))[::-1]:
13
if nums[i] == 1:
14
oneFromRight[i] = oneFromRight[i + 1] + 1
15
else:
16
oneFromRight[i] = oneFromRight[i + 1]
17

18
allSum = [0] * (len(nums) + 1)
19
currentMax = 0
20
res = []
21
for i in range(len(nums) + 1):
22
allSum[i] = oneFromRight[i] + zeroFromLeft[i]
23
if allSum[i] > currentMax:
24
res = []
25
currentMax = allSum[i]
26
if allSum[i] == currentMax:
27
res.append(i)
28
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0