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. mybatis初步配置容易出现的问题

    The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone. You ...

  2. cron 表达式0 0/10 * * * 与 0 */10 * * *的区别

    0 0/10 * * * 与 0 */10 * * * 的差别在于什么地方.在说这两者的差别之前,先说下各个字符代表的含义.0代表从0分开始,*代表任意字符,/代表递增. 0 0/10 * * *代表 ...

  3. UVALive 3977 BFS染色

    这个题意搞了半天才搞明白 就是如果定义一个d-summit,即从该点到另一个更高的点,经过的路径必定是比当前点低至少d高度的,如果该点是最高点,没有比他更高的,就直接视为顶点 其实就是个BFS染色,先 ...

  4. ETL工具对比

    ETL工具对比 Informatica Kettle 起源 1993年创立于 (美国加利福尼亚州)并于1999年4月在纳斯达克上市 2006年加入了开源BI组织  自2017年9月起,已被(日立集团下 ...

  5. SAP_MM常用代码

    1.采购申请创建/修改/查看:ME51N/ME52N/ME53N 2.采购申请审批:ME54N 3.采购订单创建/修改/查看:ME21N/ME22N/ME23N 4.单个采购订单审批:ME29N 5. ...

  6. 题解 P1220 【关路灯】

    区间DP, 考虑设\(dp[i][j][t]\)为已经关掉了\([i,j]\)的电灯, 人在t端点处时的最小代价 可以推出方程: \[ dp[i+1][j][0]+(p[n]-p[j]+p[i])*( ...

  7. 主席树的妙用——Just h-index

    题目传送门:https://ac.nowcoder.com/acm/contest/1107/C 题意:给出一个区间,求最大的 h ,使得区间内至少有 h 个数 大于等于 h. 思路:1.需要区间有序 ...

  8. Python Learning Day1

    字符串的操作 # 字符串的操作 str1 = 'my name is xxx, my age is 18.' # 优先掌握的操作: # 1.按索引取值(正向取+反向取) :只能取 print(str1 ...

  9. 第一个eclipse maven项目!我超全!

    前言:以前一直用idea做东西,今天突然想试一下,没想到配置起来是真的麻烦!!!!会出现各种各样的问题,太晚了,本文只做出几处非常严重的问题,如有疑问,请私信,留言 准备:本文     JDK 1.8 ...

  10. 操作实践:maven工程查找工程中多余的jar包

    声明:迁移自本人CSDN博客https://blog.csdn.net/u013365635 版本迭代过程中对jar的依赖可能会产生变化,一些本不必再依赖的jar包可以因为没有清除而依然留在版本的发布 ...