Skip to content

Commit 1d61f6b

Browse files
committed
MaxConsecutiveOnesII487
1 parent b8373e8 commit 1d61f6b

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

Diff for: README.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,11 @@
4444
| [Graph](https://github.com/fluency03/leetcode-java/blob/master/src/graph) |
4545

4646

47-
# Total: 296
47+
# Total: 297
4848

4949
| Easy | Medium | Hard | - |
5050
|:----:|:-------:|:----:|:-:|
51-
| 79 | 162 | 52 | 3 |
51+
| 79 | 163 | 52 | 3 |
5252

5353

5454
| Question | Solution | Difficulty |
@@ -297,6 +297,7 @@
297297
| [480. Sliding Window Median](https://leetcode.com/problems/sliding-window-median/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/SlidingWindowMedian480.java) | Hard |
298298
| [482. License Key Formatting](https://leetcode.com/problems/license-key-formatting/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/LicenseKeyFormatting482.java) | Easy |
299299
| [485. Max Consecutive Ones](https://leetcode.com/problems/max-consecutive-ones/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MaxConsecutiveOnes485.java) | Easy |
300+
| [487. Max Consecutive Ones II](https://leetcode.com/problems/max-consecutive-ones-ii/description/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MaxConsecutiveOnesII487.java) | Medium |
300301
| [490. The Maze](https://leetcode.com/problems/the-maze/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/TheMaze490.java) | Medium |
301302
| [503. Next Greater Element II](https://leetcode.com/problems/next-greater-element-ii/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/NextGreaterElementII503.java) | Medium |
302303
| [508. Most Frequent Subtree Sum](https://leetcode.com/problems/most-frequent-subtree-sum/) | [Solution](https://github.com/fluency03/leetcode-java/blob/master/src/MostFrequentSubtreeSum508.java) | Medium |

Diff for: src/MaxConsecutiveOnesII487.java

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
* Given a binary array, find the maximum number of consecutive 1s in this
3+
* array if you can flip at most one 0.
4+
*
5+
* Example 1:
6+
* Input: [1,0,1,1,0]
7+
* Output: 4
8+
* Explanation: Flip the first zero will get the the maximum number of consecutive 1s.
9+
* After flipping, the maximum number of consecutive 1s is 4.
10+
*
11+
* Note:
12+
* The input array will only contain 0 and 1.
13+
* The length of input array is a positive integer and will not exceed 10,000
14+
*
15+
* Follow up:
16+
* What if the input numbers come in one by one as an infinite stream? In other
17+
* words, you can't store all numbers coming from the stream as it's too large
18+
* to hold in memory. Could you solve it efficiently?
19+
*/
20+
21+
public class MaxConsecutiveOnesII487 {
22+
public int findMaxConsecutiveOnes(int[] nums) {
23+
if (nums == null || nums.length == 0) return 0;
24+
int left = 0;
25+
int right = 0;
26+
int max = 0;
27+
int zeros = 0;
28+
while (right < nums.length) {
29+
if (nums[right] == 1) {
30+
right++;
31+
continue;
32+
} else if (nums[right] == 0 && zeros == 0) {
33+
right++;
34+
zeros++;
35+
continue;
36+
}
37+
max = Math.max(max, right - left);
38+
while (left < nums.length && nums[left] == 1) left++;
39+
if (left < nums.length && nums[left] == 0) {
40+
left++;
41+
zeros--;
42+
}
43+
}
44+
return Math.max(max, right-left);
45+
}
46+
}

0 commit comments

Comments
 (0)