|
| 1 | +<p>Given a binary array <code>nums</code>, return <em>the maximum number of consecutive </em><code>1</code><em>'s in the array if you can flip at most one</em> <code>0</code>.</p> |
| 2 | + |
| 3 | +<p> </p> |
| 4 | +<p><strong class="example">Example 1:</strong></p> |
| 5 | + |
| 6 | +<pre> |
| 7 | +<strong>Input:</strong> nums = [1,0,1,1,0] |
| 8 | +<strong>Output:</strong> 4 |
| 9 | +<strong>Explanation:</strong> |
| 10 | +- If we flip the first zero, nums becomes [1,1,1,1,0] and we have 4 consecutive ones. |
| 11 | +- If we flip the second zero, nums becomes [1,0,1,1,1] and we have 3 consecutive ones. |
| 12 | +The max number of consecutive ones is 4. |
| 13 | +</pre> |
| 14 | + |
| 15 | +<p><strong class="example">Example 2:</strong></p> |
| 16 | + |
| 17 | +<pre> |
| 18 | +<strong>Input:</strong> nums = [1,0,1,1,0,1] |
| 19 | +<strong>Output:</strong> 4 |
| 20 | +<strong>Explanation:</strong> |
| 21 | +- If we flip the first zero, nums becomes [1,1,1,1,0,1] and we have 4 consecutive ones. |
| 22 | +- If we flip the second zero, nums becomes [1,0,1,1,1,1] and we have 4 consecutive ones. |
| 23 | +The max number of consecutive ones is 4. |
| 24 | +</pre> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong>Constraints:</strong></p> |
| 28 | + |
| 29 | +<ul> |
| 30 | + <li><code>1 <= nums.length <= 10<sup>5</sup></code></li> |
| 31 | + <li><code>nums[i]</code> is either <code>0</code> or <code>1</code>.</li> |
| 32 | +</ul> |
| 33 | + |
| 34 | +<p> </p> |
| 35 | +<p><strong>Follow up:</strong> What if the input numbers come in one by one as an infinite stream? In other words, you can't store all numbers coming from the stream as it's too large to hold in memory. Could you solve it efficiently?</p> |
0 commit comments