-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path883.max-consecutive-ones-ii.py
53 lines (49 loc) · 1.35 KB
/
883.max-consecutive-ones-ii.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
# Tag: Dynamic Programming/DP, Coordinate DP, Two Pointers
# Time: O(N)
# Space: O(1)
# Ref: Leetcode-487
# Note: -
# Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at most one 0.
#
# ```
# Example 1:
# Input: nums = [1,0,1,1,0]
# Output: 4
#
# Explanation:
# Flip the first zero will get the the maximum number of consecutive 1s.
# After flipping, the maximum number of consecutive 1s is 4.
#
# Example 2:
# Input: nums = [1,0,1,0,1]
# Output: 3
#
# Explanation:
# Flip each zero will get the the maximum number of consecutive 1s.
# After flipping, the maximum number of consecutive 1s is 3.
#
# ```
#
# 1. The input array will only contain `0` and `1`.
# 2. The length of input array is a positive integer and will not exceed `10,000`.
from typing import (
List,
)
class Solution:
"""
@param nums: a list of integer
@return: return a integer, denote the maximum number of consecutive 1s
"""
def find_max_consecutive_ones(self, nums: List[int]) -> int:
# write your code here
n = len(nums)
zero = 0
i = 0
res = 0
for j in range(n):
zero += nums[j] == 0
while zero > 1:
zero -= nums[i] == 0
i += 1
res = max(res, j - i + 1)
return res