Created
April 9, 2020 09:52
-
-
Save ianmuge/4e0d8f42e1e6e773f4a727d051ac4e05 to your computer and use it in GitHub Desktop.
This file contains 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
def solution(A): | |
if len(A) < 5: | |
return False | |
total_s = sum(A) | |
left_s,right_s = A[0],A[-1] | |
left_idx,right_idx = 1,len(A) - 2 | |
while left_idx < right_idx: | |
mid_s = total_s - left_s - right_s - (A[left_idx] + A[right_idx]) | |
if mid_s < left_s or mid_s < right_s: | |
return False | |
if left_s == mid_s == right_s: | |
return True | |
if left_s < right_s: | |
left_s += A[left_idx] | |
left_idx += 1 | |
else: | |
right_s += A[right_idx] | |
right_idx -= 1 | |
return True | |
if __name__ == '__main__': | |
results = solution([1, 3, 4, 2, 2, 2, 1, 1, 2]) | |
print(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment