Skip to content

Commit 2419fd5

Browse files
authored
Create max-consecutive-ones-iii.py
1 parent 45800a0 commit 2419fd5

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

Python/max-consecutive-ones-iii.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def longestOnes(self, A, K):
6+
"""
7+
:type A: List[int]
8+
:type K: int
9+
:rtype: int
10+
"""
11+
result, i = 0, 0
12+
for j in xrange(len(A)):
13+
K -= int(A[j] == 0)
14+
while K < 0:
15+
K += int(A[i] == 0)
16+
i += 1
17+
result = max(result, j-i+1)
18+
return result

0 commit comments

Comments
 (0)