-
Notifications
You must be signed in to change notification settings - Fork 481
/
0927.py
34 lines (29 loc) · 904 Bytes
/
0927.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution:
def threeEqualParts(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""
ones = A.count(1)
A_len = len(A)
if not ones:
return [0, A_len - 1]
if ones%3 != 0:
return [-1, -1]
step = ones//3
p, cnt = [0]*3, 0
for i, num in enumerate(A):
if num == 1:
if cnt % step == 0:
p[cnt//step] = i
cnt += 1
while p[2] < A_len and A[p[0]] == A[p[1]] and A[p[1]] == A[p[2]]:
p[0] += 1
p[1] += 1
p[2] += 1
if p[2] == A_len:
return [p[0]-1,p[1]]
return [-1, -1]
if __name__ == "__main__":
A = [1,1,1,0,1,0,0,1,0,1,0,0,0,1,0,0,1,1,1,0,1,0,0,1,0,1,0,0,0,1,0,0,0,0,1,1,1,0,1,0,0,1,0,1,0,0,0,1,0,0]
print(Solution().threeEqualParts(A))