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 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 streamTime: 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 intTime: 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)的更多相关文章
- [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 ...
- LeetCode Max Consecutive Ones II
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-ii/ 题目: Given a binary array, find the ma ...
- LeetCode——Max Consecutive Ones
LeetCode--Max Consecutive Ones Question Given a binary array, find the maximum number of consecutive ...
- 【LeetCode】487. Max Consecutive Ones II 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 日期 题目地址:https://leetco ...
- 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 ...
- [LeetCode] Max Consecutive Ones 最大连续1的个数
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 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 ...
- LeetCode: Max Consecutive Ones
这题最关键的是处理最开始连续1和最后连续1的方式,想到list一般在最前面加个node的处理方式,在最前面和最后面加0即可以很好地处理了 public class Solution { public ...
- LeetCode 1004. Max Consecutive Ones III
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/ 题目: Given an array A of 0s and 1s, w ...
随机推荐
- 微信小程序--家庭记账本开发--05
界面跳转 在微信小程序中,按钮也是<button></button>标签,它是通过bindtap属性来绑定点击事件: <view class="usermott ...
- ISP PIPLINE (三) BPC
what is the Bad Pixel? 坏点为死点,也就是基本不随照度变化呈现光电线性转换的关系.表现为暗态常亮,亮态常暗. 坏点分类:静态坏点:亮坏点,暗坏点. ...
- 在vue脚手架中使用npm的方式使用swiper
打开项目的根目录,然后打开命令窗口,输入 npm install swiper@4.4.1 @后为指定版本号,也可以不写 在main.js 中,引入 import Swiper from 'swipe ...
- 转 Using Async for File Access
原文:https://msdn.microsoft.com/en-us/library/jj155757.aspx using System; using System.Collections.Gen ...
- QT杂记(网上资料整理而成)
1.新建工程时,Qwidget和Qdialog和Qmianwindow三者的区别? QWidget是所有图形界面的基类QMainWindow是一个提供了菜单.工具条的程序主窗口QDialog是对话框. ...
- laravel之ORM增删改查数据
1.首先在控制器中添加方法,然后添加路由,接着在模型中操作: 以下是模型 2.以下是控制器中的操作 一下是通过ORM进行更新 删除数据
- 变量类型-Number
教程:一:数字类型 (1)int 没有限制大小,有以下的四种表现形式: 1:2进制:以'0b'开头---bin 2:8进制:以'0o'开头---oct 3:1 ...
- 接线端子VH,CH,XH
- linux 的基础命令
date 查看时间 cal 查看日历 cal 2009 cal 10 2019 ls 查看目录下的内容 ls -alh tree 以树的形式查看目录内容 bc 计算器 M ...
- js 获取某个某个区间内的随机整数
//获取某个某个区间内的随机整数 ,获取到的值域为[min,max)function get_random_num(min,max){ if(/^-?\d+$/.test(min) && ...