-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy path883.max-consecutive-ones-ii.cpp
55 lines (51 loc) · 1.42 KB
/
883.max-consecutive-ones-ii.cpp
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
// 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`.
class Solution {
public:
/**
* @param nums: a list of integer
* @return: return a integer, denote the maximum number of consecutive 1s
*/
int findMaxConsecutiveOnes(vector<int> &nums) {
// write your code here
int n = nums.size();
int res = 0;
int i = 0;
int zero = 0;
for (int j = 0; j < n; j++) {
zero += nums[j] == 0;
while (zero > 1) {
zero -= nums[i] == 0;
i += 1;
}
res = max(res, j - i + 1);
}
return res;
}
};