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 ...
随机推荐
- docker devise相关错误
rake aborted!Devise.secret_key was not set. Please add the following to your Devise initializer: con ...
- PAT 天梯赛 L1-037. A除以B 【水】
题目链接 https://www.patest.cn/contests/gplt/L1-037 AC代码 #include <iostream> #include <cstdio&g ...
- OpenGL学习进程(5)第三课:视口与裁剪区域
本节是OpenGL学习的第三个课时,下面介绍如何运用显示窗体的视口和裁剪区域: (1)知识点引入: 1)问题现象: 当在窗体中绘制图形后,拉伸窗体图形形状会发生变化: #include ...
- Python编程-多进程一
一.python并发编程之多进程 1.multiprocessing模块介绍 python中的多线程无法利用多核优势,如果想要充分地使用多核CPU的资源(os.cpu_count()查看),在pyth ...
- 以太坊钱包Geth使用命令
一.启动以太坊钱包Geth 打开一个控制台,执行同步区块命令 #同步测试链geth --fast --cache=512 --rpc --rpcapi personal,db,eth,net,web3 ...
- perl FAQ(zz)
1. Why do you write a program in Perl? Ans : Easy to use and fast execution since perl script underg ...
- Linux服务器注意事项
1.在Linux新建一个tomcat目录,执行里面的文件运行的时候 会出现权限不足的提示?解决办法:这是因为新建的文件夹,对于可执行脚本,必须先授权,进入bin目录后,执行命令 chmod 764 ...
- MySQL之——提示"mysql deamon failed to start"错误的解决方法
网站突然连接不上数据库,于是直接重启了一下服务器.进到cli模式下,执行 service myqsld start 发现还是提示"mysql deamon failed to start&q ...
- C# 处理base64 以及base64的原理分析
base64的原理, http://www.cnblogs.com/diligenceday/p/6002382.html http://www.cnblogs.com/chengxiaohui/ar ...
- Spring Boot入门——多文件上传大小超限问题解决
多文件上传中遇到上传文件大小的问题 org.apache.tomcat.util.http.fileupload.FileUploadBase$FileSizeLimitExceededExcepti ...