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: [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.

Note:

  • The input array will only contain 0 and 1.
  • The length of input array is a positive integer and will not exceed 10,000

Follow up:
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?

这道题在之前那道题Max Consecutive Ones的基础上加了一个条件,说我们有一次将0翻转成1的机会,问此时最大连续1的个数,再看看follow up中的说明,很明显是让我们只遍历一次数组,那我们想,肯定需要用一个变量cnt来记录连续1的个数吧,那么当遇到了0的时候怎么处理呢,因为我们有一次0变1的机会,所以我们遇到0了还是要累加cnt,然后我们此时需要用另外一个变量cur来保存当前cnt的值,然后cnt重置为0,以便于让cnt一直用来统计纯连续1的个数,然后我们每次都用用cnt+cur来更新结果res,参见代码如下:

解法一:

class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = , cur = , cnt = ;
for (int num : nums) {
++cnt;
if (num == ) {
cur = cnt;
cnt = ;
}
res = max(res, cnt + cur);
}
return res;
}
};

上面的方法有局限性,如果题目中说能翻转k次怎么办呢,我们最好用一个通解来处理这类问题。我们可以维护一个窗口[left,right]来容纳至少k个0。我们遇到了0,就累加zero的个数,然后判断如果此时0的个数大于k,那么我们我们右移左边界left,如果移除掉的nums[left]为0,那么我们zero自减1。如果不大于k,那么我们用窗口中数字的个数来更新res,参见代码如下:

解法二:

class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = , zero = , left = , k = ;
for (int right = ; right < nums.size(); ++right) {
if (nums[right] == ) ++zero;
while (zero > k) {
if (nums[left++] == ) --zero;
}
res = max(res, right - left + );
}
return res;
}
};

上面那种方法对于follow up中的情况无法使用,因为nums[left]需要访问之前的数字。我们可以将遇到的0的位置全都保存下来,这样我们需要移动left的时候就知道移到哪里了:

解法三:

class Solution {
public:
int findMaxConsecutiveOnes(vector<int>& nums) {
int res = , left = , k = ;
queue<int> q;
for (int right = ; right < nums.size(); ++right) {
if (nums[right] == ) q.push(right);
if (q.size() > k) {
left = q.front() + ; q.pop();
}
res = max(res, right - left + );
}
return res;
}
};

类似题目:

Max Consecutive Ones

参考资料:

https://discuss.leetcode.com/topic/75439/java-concise-o-n-time-o-1-space

https://discuss.leetcode.com/topic/75445/java-clean-solution-easily-extensible-to-flipping-k-zero-and-follow-up-handled

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Max Consecutive Ones II 最大连续1的个数之二的更多相关文章

  1. LeetCode Max Consecutive Ones II

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...

  2. Leetcode: Max Consecutive Ones II(unsolved locked problem)

    Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...

  3. 1004. Max Consecutive Ones III最大连续1的个数 III

    网址:https://leetcode.com/problems/max-consecutive-ones-iii/ 参考:https://leetcode.com/problems/max-cons ...

  4. LeetCode——Max Consecutive Ones

    LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...

  5. [LeetCode] Max Consecutive Ones 最大连续1的个数

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...

  6. C#LeetCode刷题之#485-最大连续1的个数(Max Consecutive Ones)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3714 访问. 给定一个二进制数组, 计算其中最大连续1的个数. ...

  7. 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...

  8. [Leetcode] Longest consecutive sequence 最长连续序列

    Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...

  9. LeetCode: Max Consecutive Ones

    这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...

随机推荐

  1. location和location.href跳转url的区别

    使用 location = url  跳转,如果本地之前已经载入过该页面并有缓存,那么会直接读取本地的缓存,缓存机制是由本地浏览器设置决定的.状态码为:  200 OK (from cache) . ...

  2. 云计算--网络原理与应用--20171122--STP与HSRP

    简单了解STP 学习HSRP 实验 一.  简单学习STP STP(spanning tree protocol)生成树协议,就是把一个环形的结构改变成一个树形的结构.通过一些算法,在逻辑上阻塞一些端 ...

  3. 【RabbitMQ系列】队列、绑定、交换器

    队列: 从概念上来讲,AMQP消息路由必须有三部分:交换器.队列和绑定.生产者把消息发布到交换器上:消息最终到达队列,并被消费者接收:绑定决定了消息如何从路由器路由到特定的队列. 消费者通过以下两种方 ...

  4. 网络推广 免费推广产品网站 B2B网站如何推广

    云集网(yunjinet.com)免费发布各类服务和产品信息,在平台上推广你的产品.帮助商家推广优质的产品和服务.如何提高信息的点击量为了提高分类信息网的信息质量,对重复度高.相似度高的信息进行了过滤 ...

  5. 百词斩APP分析

    一.调研 1.第一次上手   第一次使用,可以使用微信和qq登录感觉挺不错的不然又要注册有点麻烦,在功能上,用户可以针对自身选择不同水平的英语背单词,然后有多钟方式对自己的听力和单词翻译进行提升.在u ...

  6. 乘法表(24.9.2017) (WARNING!!!!!!!!!!!)

    #include "stdio.h" main() { int i,j,result; printf("\n"); ;i<;i++) { ;j<;j ...

  7. python实现简单tftp(基于udp)

    tftp是基于udp的协议 实现简单的tftp,首先要有tftp的协议图. tftp默认接收端口为69,但每次有连接过来后,tftp会随机分配一个端口来专门为这个连接来服务. 操作码:1.上传 2.下 ...

  8. 【iOS】OC-Quartz2D简单使用

    什么是Quartz2D Quartz 2D是一个二维绘图引擎,同时支持iOS和Mac系统 作用 ? 1 2 3 4 5 6 7 8 9 <code>Quartz 2D能完成的工作 绘制图形 ...

  9. Java面试题合集(一)

    接下来几篇文章准备系统整理一下有关Java的面试题,分为基础篇,javaweb篇,框架篇,数据库篇,多线程篇,并发篇,算法篇等等,陆续更新中. 其他方面如前端后端等等的面试题也在整理中,都会有的. 所 ...

  10. CSS <input type="file">样式设置

    这是最终想要的效果~~~ 实现很简单,div设置背景图片,<input type="file"/>绝对定位上去再设置opacity:0(透明度为0 ) 直接上代码,希望 ...