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. Windows系统自带选择文件的对话重写和居中处理

    class CMyFileDialog: public CFileDialogImpl<CMyFileDialog> { public: CMyFileDialog(BOOL bOpenF ...

  2. Egret Engine 2D - Get Started

    Get Started     Egret 也支持在命令行完成编译,运行,发布等操作.在下面的教程中会穿插对应操作的命令行代码.   可新建游戏项目,也可建eui项目   这里包含默认的几个库,egr ...

  3. java.sql.Date转换

    ---恢复内容开始--- JAVA 处理时间 - java.sql.Date.java.util.Date与数据库中的Date字段的转换方法,以及util包下的Date类与字符串的相互转换 在java ...

  4. python-局域网内实现web页面用户端下载文件,easy!

    好久没有发博客了,但是也没闲着,最近疫情原因一直在家远程办公,分享一下今天的干货 先说需求:某个文件压缩之后可以供用户点击下载 没想到特别好的办法,在网上搜索大多都是通过socket实现的,然后我这个 ...

  5. Win10电脑安装虚拟机

    版权声明:本文为CSDN博主「MHades」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明.原文链接:https://blog.csdn.net/qq_42545 ...

  6. 用UICollectionView实现上下轮播的案例

    // //  RecommendNewsCell.swift //  XMLYFM // //  Created by Domo on 2018/8/2. //  Copyright © 2018年 ...

  7. 第一章,初识C语言

    1.1 C语言起源 1.2 选择C语言的理由 1.3 C语言的应用范围 1.4 计算机能做什么 1.5 高级计算机语言和编译器 1.6 语言标准 c90,c99,c11. 1.7 使用C语言的7个步骤 ...

  8. c++ rand随机数生成(随机种子设置)

    需求:每次初始化不同的随机数 1.默认 //这样用每次都会产生相同数字 #include <stdlib.h> #include <stdio.h> #define N 10 ...

  9. ArrayList源码阅读笔记

    ArrayList ArrayList继承自AbstractList抽象类,实现了RandomAccess, Cloneable, java.io.Serializable接口,其中RandomAcc ...

  10. MFC下的网络编程(1)CAsyncSocket进行无连接(UDP)通信

    服务器端发送数据给客户端 先看服务器端: CAsyncSocket m_sockSend;                       //声明一个Socket对象 点击发送数据后,执行下面这些动作 ...