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?

未研究:

The idea is to keep a window [l, h] that contains at most k zero

The following solution does not handle follow-up, because nums[l] will need to access previous input stream
Time: O(n) Space: O(1)

    public int findMaxConsecutiveOnes(int[] nums) {
int max = 0, zero = 0, k = 1; // flip at most k zero
for (int l = 0, h = 0; h < nums.length; h++) {
if (nums[h] == 0)
zero++;
while (zero > k)
if (nums[l++] == 0)
zero--;
max = Math.max(max, h - l + 1);
}
return max;
}

Now let's deal with follow-up, we need to store up to k indexes of zero within the window [l, h] so that we know where to move lnext when the window contains more than k zero. If the input stream is infinite, then the output could be extremely large because there could be super long consecutive ones. In that case we can use BigInteger for all indexes. For simplicity, here we will use int
Time: O(n) Space: O(k)

    public int findMaxConsecutiveOnes(int[] nums) {
int max = 0, k = 1; // flip at most k zero
Queue<Integer> zeroIndex = new LinkedList<>();
for (int l = 0, h = 0; h < nums.length; h++) {
if (nums[h] == 0)
zeroIndex.offer(h);
if (zeroIndex.size() > k)
l = zeroIndex.poll() + 1;
max = Math.max(max, h - l + 1);
}
return max;
}

Note that setting k = 0 will give a solution to the earlier version Max Consecutive Ones

For k = 1 we can apply the same idea to simplify the solution. Here q stores the index of zero within the window [l, h] so its role is similar to Queue in the above solution

    public int findMaxConsecutiveOnes(int[] nums) {
int max = 0, q = -1;
for (int l = 0, h = 0; h < nums.length; h++) {
if (nums[h] == 0) {
l = q + 1;
q = h;
}
max = Math.max(max, h - l + 1);
}
return max;
}

Leetcode: Max Consecutive Ones II(unsolved locked problem)的更多相关文章

  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

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

  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: The Maze(Unsolved locked problem)

    There is a ball in a maze with empty spaces and walls. The ball can go through empty spaces by rolli ...

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

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

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

  8. LeetCode: Max Consecutive Ones

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

  9. LeetCode 1004. Max Consecutive Ones III

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

随机推荐

  1. hdu5705

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5705 题目: Problem Description Given a time HH:MM:SS an ...

  2. 翻转 -- CodeForces - 56B

    题目链接: https://cn.vjudge.net/problem/25167/origin 思路: 这是一道水题,但是一开始思路有点问题.. 1000的数据大小,直接暴搜左开始第一个与i不等的下 ...

  3. (Android UI)Android应用程序中资源:图片、字符串、颜色、布局等

    Android系统设计采用代码和布局分离的设计模式,因此在设计Android应用程序时需要遵循该设计模式. “把非代码资源(如图片和字符串常量)和代码分离开来始终是一种很好的做法.”---<An ...

  4. 更新node和npm到最新版本

    卸载 1.首先卸载nodejs,打开控制面板,然后找到程序卸载: 2.找到npm目录和npmcache 目录,直接删掉(一般情况下会在C:\Users\Caffrey\AppData\Roaming\ ...

  5. CSS3_3D 变换

    3D 变换 1. 在 2D 父元素中,绕轴旋转,效果需要想象 #ele_3d { width: transform: rotateX(2deg); } rotateX 为正,元素左上角往里跑...对象 ...

  6. [LeetCode] Design HashMap 设计HashMap

    Design a HashMap without using any built-in hash table libraries. To be specific, your design should ...

  7. Servlet实践--HelloWorld

    Servlet规范是一套技术标准,包含与Web应用相关的一系列接口,而具体的Servlet容器负责提供标准的实现,如Tomcat. Servlet的实例对象由Servlet容器负责创建,Servlet ...

  8. 关于select的id以及value传给后台的问题

    下面解释下后端为什么让传id的时候我们要怎末办? 定义一个空对象将他的值给select的值,option遍历的时候:value="item",这里的item是一个对象,也就是如果你 ...

  9. Win7 IIS配置

    一.首先,在开始打开你的控制面板 二.进入到控制面板,选择程序和功能,点击进入 三.在程序与功能中,左菜单栏,打开你的Windows工能 四.在Windows工能中,把你的Internet信息服务中的 ...

  10. JDK8之The type java.util.Map$Entry cannot be resolved

    eclipse+tomcat7+jdk1.6上面报错的方式我的解法方法是吧jre8换成6的就好了选中项目->右键->java build path ->找到jre system li ...