LeetCode 1004. Max Consecutive Ones III
原题链接在这里:https://leetcode.com/problems/max-consecutive-ones-iii/
题目:
Given an array A of 0s and 1s, we may change up to K values from 0 to 1.
Return the length of the longest (contiguous) subarray that contains only 1s.
Example 1:
Input: A = [1,1,1,0,0,0,1,1,1,1,0], K = 2
Output: 6
Explanation:
[1,1,1,0,0,1,1,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Example 2:
Input: A = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1], K = 3
Output: 10
Explanation:
[0,0,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1]
Bolded numbers were flipped from 0 to 1. The longest subarray is underlined.
Note:
1 <= A.length <= 200000 <= K <= A.lengthA[i]is0or1
题解:
Same idea as Max Consecutive Ones II.
Solution is easy to extend from 1 to K.
Time Complexity: O(n). n = A.length.
Space: O(1).
AC Java:
class Solution {
public int longestOnes(int[] A, int K) {
if(A == null || A.length == 0){
return 0;
}
int count = 0;
int walker = 0;
int runner = 0;
int res = 0;
while(runner < A.length){
if(A[runner++] != 1){
count++;
}
while(count > K){
if(A[walker++] != 1){
count--;
}
}
res = Math.max(res, runner-walker);
}
return res;
}
}
LeetCode 1004. Max Consecutive Ones III的更多相关文章
- 【LeetCode】1004. Max Consecutive Ones III 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法/双指针 日期 题目地址:https://le ...
- 【leetcode】1004. Max Consecutive Ones III
题目如下: Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of ...
- 1004. Max Consecutive Ones III最大连续1的个数 III
网址:https://leetcode.com/problems/max-consecutive-ones-iii/ 参考:https://leetcode.com/problems/max-cons ...
- LeetCode 485. Max Consecutive Ones (最长连续1)
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- 8. leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- (双指针) leetcode 485. Max Consecutive Ones
Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...
- [Swift]LeetCode1004. 最大连续1的个数 III | Max Consecutive Ones III
Given an array A of 0s and 1s, we may change up to K values from 0 to 1. Return the length of the lo ...
- LeetCode 485 Max Consecutive Ones 解题报告
题目要求 Given a binary array, find the maximum number of consecutive 1s in this array. 题目分析及思路 给定一个01数组 ...
- LeetCode: 485 Max Consecutive Ones(easy)
题目: Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: ...
随机推荐
- Http、RESTful、RPC、MQ、Socket 概念与区别
若要转载本文,请务必声明出处:https://www.cnblogs.com/zhongyuanzhao000/p/11700815.html 1. 关于HTTP: HTTP,即超文本传输协议,是一个 ...
- wildfly添加JNDI驱动配置
wildfly-9.0.1.Final/modules目录下新建com文件夹cd com && mkdir mysql && cd mysql && m ...
- Spark之RDD依赖关系及DAG逻辑视图
RDD依赖关系为成两种:窄依赖(Narrow Dependency).宽依赖(Shuffle Dependency).窄依赖表示每个父RDD中的Partition最多被子RDD的一个Partition ...
- 【leetcode-91 动态规划】 解码方法
一条包含字母 A-Z 的消息通过以下方式进行了编码: 'A' -> 1 'B' -> 2 ... 'Z' -> 26 给定一个只包含数字的非空字符串,请计算解码方法的总数. 示例 1 ...
- .net core 读取、修改配置文件appsettings.json
.net core 设置读取JSON配置文件 appsettings.json Startup.cs 中 public class Startup { public Startup(IHostingE ...
- java中四种权限修饰符区别
总的概括:public > protected > (default) > private 细分见下表格: 权限修饰符 public protected (default) priv ...
- Linux 软链接和硬链接简介
在Linux系统中,将文件分为两个部分:用户数据和元数据. 元数据(inode) 元数据即文件的索引节点(inode),用来记录文件的权限(r.w.x).文件的所有者和属组.文件的大小.文件的状态改变 ...
- permission 权限清单
<uses-permission android:name="android.permission.READ_CALENDAR" /> <uses-permiss ...
- 【面试突击】- 2019年125条常见的java面试笔试题汇总(一)
1.抽象:抽象就是忽略一个主题中与当前目标无关的那些方面,以便更充分地注意与当前目标有关的方面.抽象并不打算了解全部 问题,而只是选择其中的一部分,暂时不用部分细节.抽象包括两个方面,一是过程抽象,二 ...
- Excel工作表密码保护的破解
操作步骤:打开Visual Basic编辑器,单击“插入-->模块“,将以下代码粘贴到模块中即可. Sub DelPassword() ActiveSheet.Protect DrawingOb ...