-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontiguous_array.py
48 lines (47 loc) · 1.39 KB
/
contiguous_array.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
def findMaxLength( nums):
"""
:type nums: List[int]
:rtype: int
"""
return find_biggest(nums)
def find_biggest( remaining):
count0,count1 = 0,0
for num in remaining:
if num == 0:
count0+=1
elif num == 1:
count1+=1
if count0 == count1:
return len(remaining)
else:
if count0 > count1:
#remove the 0 closest to one of the ends
removed = False
start,end = 0, len(remaining)
while not removed:
if remaining[start] == 0:
start+=1
removed = True
elif remaining[end-1] == 0:
end-=1
removed = True
else:
start+=1
end-=1
return find_biggest(remaining[start:end])
elif count1> count0:
#remove closest 1 to the ends
removed = False
start,end = 0, len(remaining)
while not removed:
if remaining[start] == 1:
start+=1
removed = True
elif remaining[end-1] == 1:
end-=1
removed = True
else:
start+=1
end-=1
return find_biggest(remaining[start:end])
print findMaxLength([0,1,1])