-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1004.py
65 lines (51 loc) · 1.53 KB
/
1004.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
class Solution(object):
def longestOnes(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
t = K
start = 0
end = 0
n = len(A)
sz = float("-inf")
if t < 0:
return 0
if t ==0 :
while(end < n):
while start< n and A[start]==0:
start+=1;
end=start
while(end < n and A[end]!=0):
end+=1
sz = max(sz, end - start)
start=end
else:
while(True):
while(t>0 and end < n):
if A[end]==0:
t-=1
end+=1
if t==0:
while end < n and A[end]!=0:
end+=1
if t ==0:
sz = max(end-start, sz)
if end >= n:
break
while(t==0 and start < end):
if A[start]==0:
t+=1
start+=1
if sz == float("-inf") and t != 0:
sz = n
return sz
if __name__ == "__main__":
s = Solution()
#print(s.longestOnes([0,0,1,1,1,0,0],0))
print(s.longestOnes([1,1,1,0,0,0,1,1,1,1],0))
#print(s.longestOnes([0,0,0,1], 4))
#print(s.longestOnes([0,0,1,1,1,0,0],0))
#print(s.longestOnes([1,1,1,0,0,0,1,1,1,1,0], 2))
#print(s.longestOnes([0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], 3))