[Algo] 625. Longest subarray contains only 1s
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的更多相关文章
- 2019杭电多校第二场hdu6602 Longest Subarray(线段树)
Longest Subarray 题目传送门 解题思路 本题求一个最大的子区间,满足区间内的数字要么出现次数大于等于k次,要么没出现过.给定区间内的数字范围是1~c. 如果r为右边界,对于一种数字x, ...
- HDU6602 Longest Subarray hdu多校第二场 线段树
HDU6602 Longest Subarray 线段树 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6602 题意: 给你一段区间,让你求最长的区间使 ...
- Longest subarray of target sum
2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...
- [Algo] 253. Longest Substring Without Repeating Characters
Given a string, find the longest substring without any repeating characters and return the length of ...
- 2019年杭电多校第二场 1012题Longest Subarray(HDU6602+线段树)
题目链接 传送门 题意 要你找一个最长的区间使得区间内每一个数出现次数都大于等于\(K\). 思路 我们通过固定右端点考虑每个左端点的情况. 首先对于每个位置,我们用线段树来维护它作为\(C\)种元素 ...
- 2019杭电多校二 L. Longest Subarray (线段树)
大意: 给定序列$a$, 元素范围$[1,C]$, 求一个最长子序列, 满足每个元素要么不出现, 要么出现次数$\le K$. 枚举右端点, 考虑左端点合法的位置. 显然一定是$C$种颜色合法位置的交 ...
- Longest Subarray(HDU6602+线段树)
题意 要你找一个最长的区间使得区间内每一个数出现次数都大于等于K. 题解->https://blog.csdn.net/Ratina/article/details/97503663 #incl ...
- [2019杭电多校第二场][hdu6602]Longest Subarray(线段树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6602 题目大意为求最长的区间,满足C种数字在区间内要么不出现,要么出现的次数都不小于K. 大致的分析一 ...
- 【HDOJ6602】Longest Subarray(线段树,vector)
题意:给定一个长为n的序列,第i个数a[i]都是一个[1,c]中的整数 如果一段序列[l,r]中出现过的数字出现次数都>=K则称其为好的序列 求最长的好的序列的长度 n,k,c,a[i]< ...
随机推荐
- 如何创建NPM包并上传
1 在NPM网站上注册,并验证(verify)自己的邮箱 https://www.npmjs.com 2 用命令行定位到你的库文件夹 3 在命令行里登录npm, 按提示依次输入用户名 密码 注册邮箱 ...
- 19 01 17 Django 模板 返回一个页面
模板 问题 如何向请求者返回一个漂亮的页面呢? 肯定需要用到html.css,如果想要更炫的效果还要加入js,问题来了,这么一堆字段串全都写到视图中,作为HttpResponse()的参数吗?这样定义 ...
- PHP循环语句练习题
<?php //输出0-100所有数字 for ($i=0; $i <101 ; $i++) { echo($i . '<br />'); }; //输出水仙花数 for ($ ...
- 八、CI框架之输出URI路径,相当于GET
一.controller中的代码如下: 二.我们打开一个路径,输出对应的路径URI的值 http://127.0.0.1/CodeIgniter-3.1.10/index.php/welcome/in ...
- SourceTree - 对Git的使用
SourceTree - 对Git的使用 一 .SourceTree简介 SourceTree 是 Windows 和Mac OS X 下免费的 Git 和 Hg 客户端,拥有可视化界面,容易上手操作 ...
- 静听网+python爬虫+多线程+多进程+构建IP代理池
目标网站:静听网 网站url:http://www.audio699.com/ 目标文件:所有在线听的音频文件 附:我有个喜好就是听有声书,然而很多软件都是付费才能听,免费在线网站虽然能听,但是禁ip ...
- MySQL-半同步复制原理实践
参考文档: http://mysql.taobao.org/monthly/2017/04/01/ 阿里内核月报半同步复制的数据一致性 https://www.cnblogs.com/ivictor/ ...
- @EnableAutoConfiguration激活自动装配
给予上个例子,将WebConfiguration类上的@SpringBootApplication换成@EnableAutoConfiguration.启动并运行http://localhost:80 ...
- VScode 修改中文字体
打开vscode ctrl+,打开设置 找到font,第一个是首选的英文字体,第二个是中文字体.
- 数据可视化BI平台——CBoard的部署与使用(笔记整理)
CBoard作为国内自主开发的数据可视化平台,因其方便好用而受到广大用户的使用和好评.现今CBoard有社区版和企业版两个版本,本文所述为社区版的0.4.2版本.注意:所需的一切资源以及相关参考链接都 ...