Given an array of integers that contains only 0s and 1s and a positive integer k, you can flip at most k 0s to 1s, return the longest subarray that contains only integer 1 after flipping.

Assumptions:

1. Length of given array is between [1, 20000].

2. The given array only contains 1s and 0s.

3. 0 <= k <= length of given array.

Example 1:

Input: array = [1,1,0,0,1,1,1,0,0,0], k = 2

Output: 7

Explanation: flip 0s at index 2 and 3, then the array becomes [1,1,1,1,1,1,1,0,0,0], so that the length of longest subarray that contains only integer 1 is 7.

Example 2:

Input: array = [1,1,0,0,1,1,1,0,0,0], k = 0

Output: 3

Explanation: k is 0 so you can not flip any 0 to 1, then the length of longest subarray that contains only integer 1 is 3.

public class Solution {
public int longestConsecutiveOnes(int[] nums, int k) {
// Write your solution here
int i = 0;
int start = 0;
int count = 0;
int result = 0;
while (i < nums.length) {
if (nums[i] == 0) {
count += 1;
}
while (count > k) {
if (nums[start] == 0) {
count -= 1;
}
start += 1;
}
i += 1;
result = Math.max(result, i - start);
}
return result;
}
}

[Algo] 625. Longest subarray contains only 1s的更多相关文章

  1. 2019杭电多校第二场hdu6602 Longest Subarray(线段树)

    Longest Subarray 题目传送门 解题思路 本题求一个最大的子区间,满足区间内的数字要么出现次数大于等于k次,要么没出现过.给定区间内的数字范围是1~c. 如果r为右边界,对于一种数字x, ...

  2. HDU6602 Longest Subarray hdu多校第二场 线段树

    HDU6602 Longest Subarray 线段树 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6602 题意: 给你一段区间,让你求最长的区间使 ...

  3. Longest subarray of target sum

    2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...

  4. [Algo] 253. Longest Substring Without Repeating Characters

    Given a string, find the longest substring without any repeating characters and return the length of ...

  5. 2019年杭电多校第二场 1012题Longest Subarray(HDU6602+线段树)

    题目链接 传送门 题意 要你找一个最长的区间使得区间内每一个数出现次数都大于等于\(K\). 思路 我们通过固定右端点考虑每个左端点的情况. 首先对于每个位置,我们用线段树来维护它作为\(C\)种元素 ...

  6. 2019杭电多校二 L. Longest Subarray (线段树)

    大意: 给定序列$a$, 元素范围$[1,C]$, 求一个最长子序列, 满足每个元素要么不出现, 要么出现次数$\le K$. 枚举右端点, 考虑左端点合法的位置. 显然一定是$C$种颜色合法位置的交 ...

  7. Longest Subarray(HDU6602+线段树)

    题意 要你找一个最长的区间使得区间内每一个数出现次数都大于等于K. 题解->https://blog.csdn.net/Ratina/article/details/97503663 #incl ...

  8. [2019杭电多校第二场][hdu6602]Longest Subarray(线段树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6602 题目大意为求最长的区间,满足C种数字在区间内要么不出现,要么出现的次数都不小于K. 大致的分析一 ...

  9. 【HDOJ6602】Longest Subarray(线段树,vector)

    题意:给定一个长为n的序列,第i个数a[i]都是一个[1,c]中的整数 如果一段序列[l,r]中出现过的数字出现次数都>=K则称其为好的序列 求最长的好的序列的长度 n,k,c,a[i]< ...

随机推荐

  1. JAVAEE 和项目开发(第二课:HTTP协议的特点和交互流程)

    HTTP 的概念和介绍 概念:超文本传输协议(Hyper Text Transfer Protocol) 作用:规范了浏览器和服务器的数据交互 特点: 简单快速:客户向服务器请求服务时,只需传送请求方 ...

  2. 2020PHP面试-PHP篇

    一.列举一些PHP的设计模式 单例模式:保证在整个应用程序的生命周期中,任何一个时刻,单例类的实例都只存在一个,同时这个类还必须提供一个访问该类的全局访问点. 工厂模式:定义一个创建对象的接口,但是让 ...

  3. INSTALL_FAILED_SHARED_USER_INCOMPATIBLE错误解决

    Target device: smartisan-yq601-3fa1a5dcInstalling APK: /Users/wangliang/workspace/emm-android/build/ ...

  4. 洛谷 P1220 关路灯(区间dp,前缀和)

    传送门 解题思路 先明确一下题意,c指的是路灯的编号而不是位置. 然后根据贪心,在从点i去关点j的路灯时,所有经过的路灯都会随手关掉(不耗时间),所以我们可以确定,若i点和j点的路灯已经关闭,那么区间 ...

  5. 学习spring的第一天

    1.首先在maven repository中找到Spring Context依赖添加进模块 2.配置xml,resources右键new→xml configuration file→Spring C ...

  6. error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMT

    项目--属性 ---连接器---命令行 输入: /FORCE:MULTIPLE 编译环境:VS2012SP3

  7. ..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init (referred from exti.o). 错误修改

    今天在移植野火的程序到元子的开发平台上时候,发现自己在中断初话中断函数的时候出现了:..\OBJ\LED.axf: Error: L6218E: Undefined symbol EXTI_Init ...

  8. kubele常用配置

    KUBELET_OPTS="--logtostderr=true \--v=4 \--hostname-override=10.83.52.147 \--kubeconfig=/usr/lo ...

  9. Maven:A cycle was detected in the build path of project 'xxx'. The cycle consists of projects {xx}

    以下这个错误是在Eclipse中导入多个相互依赖的工程时出现的“循环依赖问题”:A cycle was detected in the build path of project 'xxx'. The ...

  10. Java哪些集合类是线程安全的?

    早在jdk的1.1版本中,所有的集合都是线程安全的.但是在1.2以及之后的版本中就出现了一些线程不安全的集合,为什么版本升级会出现一些线程不安全的集合呢?因为线程不安全的集合普遍比线程安全的集合效率高 ...