1004. Max Consecutive Ones III
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 66% Topics: Array, Binary Search, Sliding Window, Prefix Sum
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
class Solution(object):
def longestOnes(self, A, K):
"""
:type A: List[int]
:type K: int
:rtype: int
"""
result, i = 0, 0
for j in xrange(len(A)):
K -= int(A[j] == 0)
while K < 0:
K += int(A[i] == 0)
i += 1
result = max(result, j-i+1)
return result
Solution from kamyu104/LeetCode-Solutions · MIT