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 ...
随机推荐
- css结构选择器组合使用,选择父元素中多个子元素中某一段元素
nth-of-type()和nth-child()写法一样,这里只用nth-of-type()演示,习惯type 直接上代码 /* 从前向后选择,第6个开始 */ li:nth-of-type(n+6 ...
- DDoS攻击与防御(4)
在发生DDoS攻击的情况下,可以通过一些缓解技术来减少攻击对自身业务和服务的影响,从而在一定程度上保障业务正常运行.缓解DDoS攻击的主要方法是对网络流量先进行稀释再进行清洗. 1.攻击流量的稀释 1 ...
- angularjs 设置全局变量(constant)
转:angularjs自身有二种,设置全局变量的方法,在加上js的设置全局变量的方法,总共有三种.要实现的功能是,在ng-app中定义的全局变量,在不同的ng-controller里都可以使用. 1, ...
- Team
队名 正义联盟 团队成员 211606301 蔡振翼(队长) 211606445 肖志豪 211606382 吴文清 211606370 刘华强 211605240 谢孟轩 211614269 林凯 ...
- 简单的3d变换
<!DOCTYPE html><html> <head> <meta charset="UTF-8"> <title> ...
- Cmake知识----编写CMakeLists.txt文件编译C/C++程序
1.CMake编译原理 CMake是一种跨平台编译工具,比make更为高级,使用起来要方便得多.CMake主要是编写CMakeLists.txt文件,然后用cmake命令将CMakeLists.txt ...
- java判断通常的逻辑
package com.stylefeng.guns.core.common.constant.factory; import com.baomidou.mybatisplus.mapper.Enti ...
- JavaScript 特效之四大家族(offset/scroll/client/event)
三大系列:offset.scroll.client 事件对象:event(事件被触动时,鼠标和键盘的状态)(通过属性控制) 三大系列都是以DOM元素节点的属性形式存在的. 类比访问关系,也是以 ...
- C# int[,] 和 int[][]
int[] 一维数组 int[,] 二维数组 int[] [] 交错数组 又称“数组的数组” 一维数组声明与初始化 声明: int[] a = new int[]; 声明与初始化: int arr ...
- vue组件创建学习总结
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...