[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
0
and1
. - 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 ...
随机推荐
- redis 相关知识
1. 什么是Redis Redis是由意大利人Salvatore Sanfilippo(网名:antirez)开发的一款内存高速缓存数据库.Redis全称为:Remote Dictionary Ser ...
- lua向文件中写入数据,进行记录
function readfile(path) local file = io.open(path, "r") if file then local content = file: ...
- 一台windows主机上运行2个tomcat
为了运行2个不同的项目,需要在一台机上运行2个tomcat,但是发现运行第二个tomcat时,总会加载第一个tomcat中的项目,也就是实际运行的是第一个tomcat 所以需要做如下配置: 1.修改第 ...
- C语言第二次博客作业—分支结构
一.PTA实验作业 题目1:计算分段函数 1.实验代码 double x,y; scanf("%lf",&x); if(x>=0){ y=sqrt(x); print ...
- Archlinux无线联网教程
本人是学生党,故对于有线方式不甚了解,学校里一般使用mentohust用动态IP方式联网,或者直接连接wifi,这里介绍无线联网的一些方式,主要包括公共wifi和带有WEP或者WPA或者WPA2PSK ...
- (转)如何在Eclipse中查看JDK类库的源代码
在Eclipse中查看JDK类库的源代码!!! 设置: 1.点 “window”-> "Preferences" -> "Java" -> & ...
- 《高级软件测试》11.16.Jira使用说明的撰写和操作视频的录制
今日任务完成情况如下: 小王:完成了测试管理工具jira的使用手册中,基本情况介绍.下载安装部分的撰写工作:小高:参考官方手册,结合自己的实际使用体会,对jira的基本组成及其工作流程进行了介绍:小陈 ...
- php中(包括织梦cms)set_time_limit(0)不起作用的解决方法
背景介绍: 在做织梦冗余图片清理的功能时, 由于冗余图片太多,导致每次清理时都会超时, 后来在网上搜索了各种文章,网上有如下的解决方法: set_time_limit(0) ini_set('max_ ...
- windows安装gcc编译器
由于vc6.0对c语言编译不是很好,有些语句是正确的,但是编译却不能通过 所以决定在windows中安装gcc编译器来使用! http://www.cnblogs.com/cryinstall/arc ...
- 链家2018春招Java工程师编程题题解
Light 题目描述 在小红家里面,有n组开关,触摸每个开关,可以使得一组灯泡点亮.现在问你,使用这n组开关,最多能够使得多少个灯泡点亮呢? 输入 第一行一个n,表示有n组开关.接下来n行,每行第一个 ...