Last active
December 10, 2019 03:00
-
-
Save liketheflower/9e8dbefee98828abeef2c5303296007c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution: | |
def threeEqualParts(self, A: List[int]) -> List[int]: | |
S = ''.join([str(a) for a in A]) | |
cnt_1 = S.count('1') | |
if cnt_1 == 0:return [0, 2] | |
# each part should have the same number of '1' | |
if cnt_1 % 3!=0:return [-1, -1] | |
counter_1, valid_part = 0, '' | |
for i, s in enumerate(S[::-1]): | |
if s=='1':counter_1+=1 | |
valid_part += s | |
if counter_1==cnt_1//3:break | |
valid_part = valid_part[::-1] | |
N = len(valid_part) | |
first_second = S[:-N] | |
# we can remove leading zeros by collecting this information | |
idx_1 = first_second.find('1') | |
if first_second[idx_1:].startswith(valid_part): | |
second = first_second[idx_1:][N:] | |
idx_1_ = second.find('1') | |
if second[idx_1_:].startswith(valid_part): | |
return [idx_1+N-1, idx_1+N+idx_1_+N] | |
return [-1, -1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment