1
class Solution:
2
def threeEqualParts(self, arr: List[int]) -> List[int]:
3
n = len(arr)
4
count_one = arr.count(1)
5
if count_one == 0:
6
return [0, n - 1]
7
if count_one % 3 != 0:
8
return [-1, -1]
9
target_ones = count_one // 3
10
breaks = []
11
one_count = 0
12
for i, bit in enumerate(arr):
13
if bit == 1:
14
one_count += 1
15
if one_count in [1, target_ones + 1, 2 * target_ones + 1]:
16
breaks.append(i)
17
if one_count in [target_ones, 2 * target_ones, 3 * target_ones]:
18
breaks.append(i)
19
i1, j1, i2, j2, i3, j3 = breaks
20

21
if not arr[i1 : j1 + 1] == arr[i2 : j2 + 1] == arr[i3 : j3 + 1]:
22
return [-1, -1]
23

24
trailing_zeroes_left = i2 - j1 - 1
25
trailing_zeroes_mid = i3 - j2 - 1
26
trailing_zeroes_right = n - j3 - 1
27
if trailing_zeroes_right > min(trailing_zeroes_left, trailing_zeroes_mid):
28
return [-1, -1]
29
j1 += trailing_zeroes_right
30
j2 += trailing_zeroes_right
31
return [j1, j2 + 1]

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0