LeetCode Max Consecutive Ones II
原题链接在这里: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
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进阶题目.
用快慢指针. 每当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 Characters, Max Consecutive Ones III.
LeetCode Max Consecutive Ones II的更多相关文章
- [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(unsolved locked problem)
Given a binary array, find the maximum number of consecutive 1s in this array if you can flip at mos ...
- 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] 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 ...
- 485. Max Consecutive Ones最长的连续1的个数
[抄题]: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Inpu ...
随机推荐
- SPOJ - HORRIBLE 【线段树】
思路 线段树 区间更新 模板题 注意数据范围 AC代码 #include <cstdio> #include <cstring> #include <ctype.h> ...
- iOS iPhone X 适配启动图片
刚出了Xcode9正式版 迫不及待地下载 使用了 保密了这么久的iPhone X 模拟器 运行起来这个样子 上下都留白不正常 这必须匹配新的启动图才行,马上查苹果开发文档 get it!!!! 添加了 ...
- 【HackerRank】Service Lane
Calvin is driving his favorite vehicle on the 101 freeway. He notices that the check engine light o ...
- 跨平台移动开发_Android 平台使用 PhoneGap 方法
PhoneGap 下载地址http://phonegap.com/install/ 1.打开 Eclipse,在文件菜单下面点击 创建> Android Application Proj ...
- nodejs异步调用async
犹豫nodejs是异步编程模型,有一些在同步编程中很容易做到的事情,现在却变得很麻烦,async的流程控制就是为了简化这些操作var async = require('async'); 1.serie ...
- 20145240 《Java程序设计》第五周学习总结
20145240 <Java程序设计>第五周学习总结 教材学习内容总结 语法与继承结构 8.1.1使用try.catch java中所有的错误都会被打包为对象,并提供了特有的语句进行处理. ...
- Anaconda创建环境、删除环境、激活环境、退出环境
Anaconda创建环境: //下面是创建python=3.6版本的环境,取名叫py36 conda create -n py36 python=3.6 删除环境(不要乱删啊啊啊) conda re ...
- sem总结
从实况搜索这些关键词 有没有排名,有排名 不用管,没有的话 就一点点网上加价格 ,加到有为止 一个单元关键字控制30以内差不多 如果关键词词量有限 ,病种相差不大 可以不用屏蔽 投产=销量/消费订购率 ...
- wareshark网络协议分析之ARP
一.ARP协议简介 简单的说ARP协议就是实现ip地址到物理地址的映射.当一台主机把以太网数据帧发送到位于同一局域网上的另一台主机时,是根据48bit的以太网地址(物理地址)来确定网络接口的. ARP ...
- eclipse安装hibernate插件(在线Marketplace中安装)
网上很多都是给个网址,然后在eclipse的help中new install soft中安装.每次安装还要去查找最新的地址去安装.为什么不用eclipse的marketplace直接搜索安装呢? 打开 ...