Skip to content

Commit f6229a4

Browse files
authored
Create three-equal-parts.py
1 parent 0431b5e commit f6229a4

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

Python/three-equal-parts.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def threeEqualParts(self, A):
6+
"""
7+
:type A: List[int]
8+
:rtype: List[int]
9+
"""
10+
total = sum(A)
11+
if total % 3 != 0:
12+
return [-1, -1]
13+
if total == 0:
14+
return [0, len(A)-1]
15+
16+
count = total//3
17+
nums = [0]*3
18+
c = 0
19+
for i in xrange(len(A)):
20+
if A[i] == 1:
21+
if c % count == 0:
22+
nums[c//count] = i
23+
c += 1
24+
25+
while nums[2] != len(A):
26+
if not A[nums[0]] == A[nums[1]] == A[nums[2]]:
27+
return [-1, -1]
28+
nums[0] += 1
29+
nums[1] += 1
30+
nums[2] += 1
31+
return [nums[0]-1, nums[1]]

0 commit comments

Comments
 (0)