Skip to content

Commit 9a6a2e8

Browse files
committed
927
1 parent cc2102c commit 9a6a2e8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

code/927.three-equal-parts.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#
2+
# @lc app=leetcode id=927 lang=python3
3+
#
4+
# [927] Three Equal Parts
5+
#
6+
7+
# @lc code=start
8+
class Solution:
9+
def threeEqualParts(self, A: List[int]) -> List[int]:
10+
11+
num_ones = sum(A)
12+
13+
if num_ones == 0:
14+
return [0, 2]
15+
16+
if num_ones % 3 != 0:
17+
return [-1, -1]
18+
19+
c = 0
20+
s1 = s2 = s3 = -1
21+
for idx,x in enumerate(A):
22+
# Find the first 1 in each part
23+
if x == 1:
24+
c += 1
25+
26+
if c == 1 and s1 < 0:
27+
s1 = idx
28+
29+
if c == num_ones//3 + 1 and s2 < 0:
30+
s2 = idx
31+
32+
if c == num_ones*2//3 + 1 and s3 < 0:
33+
s3 = idx
34+
break
35+
36+
n = len(A[s3:]) # The length of each part when all the leading 0's are removed
37+
38+
if A[s1:s1+n] == A[s2:s2+n] and A[s2:s2+n] == A[s3:]:
39+
return [s1+n-1, s2+n]
40+
else:
41+
return [-1, -1]
42+
# @lc code=end
43+

0 commit comments

Comments
 (0)