原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/

题目:

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进阶题目.

用快慢指针. 每当nums[runner]是0时, zero count 加一.

When zero count 大于可以flip最大数目时, move walker, 每当nums[walker]等于0, zero count 减一直到zero count 等于k.

同时维护最大值res = Math.max(res, runner-walker).

Time Complexity: O(nums.length). Space: O(1).

AC Java:

 class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
} int res = 0;
int count = 0;
int walker = 0;
int runner = 0;
while(runner < nums.length){
if(nums[runner++] != 1){
count++;
} while(count > 1){
if(nums[walker++] != 1){
count--;
}
} res = Math.max(res, runner-walker);
} return res;
}
}

Follow up说input是infinite stream, 不能把整个array放在memory中.

When calculating res, can't move walker, since stream may be out of memory already.

可以只用queue来记录等于0的index即可. 当queue.size() > k表示0的数目超过了可以flip的最大值,所以要dequeue.

Time Complexity: O(n). Space: O(k).

AC Java:

 class Solution {
public int findMaxConsecutiveOnes(int[] nums) {
if(nums == null || nums.length == 0){
return 0;
} int res = 0;
int walker = 0;
int runner = 0;
LinkedList<Integer> que = new LinkedList<>();
while(runner < nums.length){
if(nums[runner++] != 1){
que.add(runner);
} while(que.size() > 1){
walker = que.poll();
} res = Math.max(res, runner-walker);
} return res;
}
}

类似Longest Substring with At Most Two Distinct CharactersMax Consecutive Ones III.

LeetCode Max Consecutive Ones II的更多相关文章

  1. [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 mos ...

  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. LeetCode——Max Consecutive Ones

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

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

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

  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. 487. Max Consecutive Ones II

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

  7. LeetCode: Max Consecutive Ones

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

  8. LeetCode 1004. Max Consecutive Ones III

    原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...

  9. 485. Max Consecutive Ones最长的连续1的个数

    [抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...

随机推荐

  1. Objective-C系列总结之基础知识

    //第一个程序示例 #import <Foundation/Foundation.h> int main(int argc,const char * argv[]) { @autorele ...

  2. 吐槽 MySQL数据库jdbc操作,varchar类型占位符问题——单引号造孽

    很长时间不写代码动手能力明显下降很多常见的错误还是经常发生,今天吐血了一次. 简单的坑总是要多跳几次才能甘心.很清晰的记得大学的时候在此坑差点闷死,现在又跳进这个坑了,搞了半天终于知道错在哪里. St ...

  3. Linux sh远程连接失败 sshd.service启动失败

    今天不小心在本机的虚拟机执行了 chmod -R 777 /var 导致/var目录下所有权限全部是777 其中 /var/empty/sshd目录权限必须是744,却被改成了777,致使sh远程连接 ...

  4. 一句white-space:nowrap解决IE6,IE7下浮动元素不自动换行

    一句white-space:nowrap解决IE6,IE7下浮动元素不自动换行

  5. Cocos2d-x项目移植到WP8系列之五:播放MP3

    原文链接: http://www.cnblogs.com/zouzf/p/3972549.html 这一块的细节还是不太了解,只是东凑西拼能跑起来而已 1.网上下载lamb库 生成需要的lib库,详情 ...

  6. nginx location proxy pass

    nginx: 192.168.1.23作为nginx反向代理机器 目标机器192.168.1.5上部署一个8090端口的nginx [root@localhost conf.d]# cat test. ...

  7. 关于centos7下/etc/sysconfig/目录没有iptables问题

    在新买的centos7服务器中想打开防火墙,采用传统centos6的方式用service iptables restart/stop/status 之后报错: 而在/etc/sysconfig/目录下 ...

  8. NCBI

    RefSeq: NCBI Reference Sequences GeneBank序列注释说明 利用NCBI查找基因信息 NCBI中RefSeq各种accession说明(一) NCBI中RefSeq ...

  9. tensorFlow 神经网络2

    learnrate 太大容易跑飞,设置激活函数 可以一定程度上增加learnrate,不跑飞 self.saver = tf.train.Saver() 和 self.init_variable = ...

  10. 权限【TLCL】

    用户帐户 定义在/etc/passwd 文件里面,用户组定义在/etc/group 文件里面. /etc/shadow 包含了关于用户密码的信息 文件属性: 属性 文件类型 - 一个普通文件 d 一个 ...