[
array
dynamic programming
sliding window
]
BinarySearch 0803 Longest 1s After One Flip
Problem statement
https://binarysearch.com/problems/Longest-1s-After-One-Flip/
Solution
Equal to Leetcode 0487. Max Consecutive Ones II.
Complexity
Time complexity is O(n)
, space complexity is O(1)
.
Code
class Solution:
def solve(self, A):
ans, n, zeroes = 0, len(A), 0
beg, end = 0, 0
while end < n:
if end < n and zeroes + (A[end] == "0") <= 1:
zeroes += (A[end] == "0")
end += 1
ans = max(ans, end - beg)
else:
zeroes -= (A[beg] == "0")
beg += 1
return ans