[LeetCode] Max Consecutive Ones II 最大连续1的个数之二
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
0and1. - 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;
}
};
类似题目:
参考资料:
https://discuss.leetcode.com/topic/75439/java-concise-o-n-time-o-1-space
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Max Consecutive Ones II 最大连续1的个数之二的更多相关文章
- LeetCode Max Consecutive Ones II
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...
- 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 ...
- 1004. Max Consecutive Ones III最大连续1的个数 III
网址:https://leetcode.com/problems/max-consecutive-ones-iii/ 参考:https://leetcode.com/problems/max-cons ...
- LeetCode——Max Consecutive Ones
LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...
- [LeetCode] Max Consecutive Ones 最大连续1的个数
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- C#LeetCode刷题之#485-最大连续1的个数(Max Consecutive Ones)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3714 访问. 给定一个二进制数组, 计算其中最大连续1的个数. ...
- 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- [Leetcode] Longest consecutive sequence 最长连续序列
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- LeetCode: Max Consecutive Ones
这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...
随机推荐
- KVM之二:配置网络
1.安装KVM a.通过yum安装虚拟化的软件包 [root@kvm ~ ::]#yum install -y kvm virt-* libvirt bridge-utils qemu-img 说明: ...
- New UWP Community Toolkit - Markdown
概述 前面 New UWP Community Toolkit 文章中,我们对 V2.2.0 版本的重要更新做了简单回顾,其中简单介绍了 MarkdownTextBlock 和 MarkdownDoc ...
- 解决fiddler无法抓取本地部署项目的请求问题
在本地部署了几个应用,然后想用fiddler抓取一些请求看看调用了哪些接口,然鹅,一直抓不到... 比如访问地址是这样的: 在网上搜罗半天,找到一个解决方法 在localhost或127.0.0.1后 ...
- Beta No.5
今天遇到的困难: 前端大部分代码由我们放逐的组员完成,这影响到了我们解决"Fragment碎片刷新时总产生的固定位置"的进程,很难找到源码对应 新加入的成员对界面代码不熟悉. 我们 ...
- 《Language Implementation Patterns》之 构建语法树
如果要解释执行或转换一段语言,那么就无法在识别语法规则的同时达到目标,只有那些简单的,比如将wiki markup转换成html的功能,可以通过一遍解析来完成,这种应用叫做 syntax-direct ...
- 利用python实现简单登陆注册系统
#!/usr/bin/env python # -*- coding:utf-8 -*- def login(username,password): ''' :param username:用户名 : ...
- 【iOS】OC-UTC日期字符串格式化
NSLog(@"%@",[NSDate date]); NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init ...
- 大数据技术Hadoop笔试题
Hadoop有高容错性的特点,并且设计用来部署在低廉的(low-cost)硬件上.以下是由应届毕业生网小编J.L为您整理推荐的面试笔试题目和经验,欢迎参考阅读. 单项选择题 1. 下面哪个程序负责 H ...
- HAOI 2012 高速公路
https://www.luogu.org/problem/show?pid=2221 题目描述 Y901高速公路是一条重要的交通纽带,政府部门建设初期的投入以及使用期间的养护费用都不低,因此政府在这 ...
- Vue 爬坑之路(十一)—— 基于 Nuxt.js 实现服务端渲染(SSR)
直接使用 Vue 构建前端单页面应用,页面源码时只有简单的几行 html,这并不利于网站的 SEO,这时候就需要服务端渲染 2016 年 10 月 25 日,zeit.co 背后的团队对外发布了一个 ...