HackerRank-Longest Subarray
give an array and target value, find the max length of the subarray which sum of the elements is less or equal to that value.
public int longestSubArray(int[] a, int k){
if(a==null || a.length==0){
return 0;
}
int max=0;
int len=a.length;
for(int i=0; i<len; i++){
int start=a[i];
if(start >= k){
if(max<1){
max=1;
}
}
else{
int sum=start;
for(int j=i+1; j<len; j++){
sum=sum+a[j];
if(sum>k){
if(max<(j-i)){
max=j-i;
}
break;
}
if(sum <= k && j==(len-1)){
if(max<(j-i+1)){
max=j-i+1;
}
}
}
}
}
return max;
}
HackerRank-Longest Subarray的更多相关文章
- 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 题意: 给你一段区间,让你求最长的区间使 ...
- [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 mo ...
- Longest subarray of target sum
2018-07-08 13:24:31 一.525. Contiguous Array 问题描述: 问题求解: 我们都知道对于subarray的问题,暴力求解的时间复杂度为O(n ^ 2),问题规模已 ...
- 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]< ...
- 2019HDU多校训练第二场 Longest Subarray
题意:给你一个串,问满足以下条件的子串中最长的是多长:对于每个数字,要么在这个子串没出现过,要么出现次数超过k次. 思路:对于这类问题,常常转化为数据结构的询问问题.我们考虑枚举右端点,对于当前右端点 ...
随机推荐
- UliPad ----python 开发利器
安装wxPython ...
- Fiddler-2 Fiddler抓包原理
1 fiddler抓包是在 客户端和服务器之间建立一个代理服务器,监听本机发出的请求和服务器返回的响应结果. 截一张官网的图: 2 启动fiddler之前,[dinghanhua]先来看一下代理服务器 ...
- windows平台源码编译最新版openssl
本文有问题,待改中................. 1.从openssl官网下载最新版openssl https://www.openssl.org/source/ The latest ...
- Win8 安装 Scrapy
安装Python2.7.11 32位(自带pip) 使用如下命令更新pip python -m pip install -U pip 下载lxml,建议32位,直接安装 https://pypi.py ...
- 删除hao123这个恶心的毒瘤
最近做服务器,好好一个东西莫名其妙的被染上了这个狗皮膏药......然后我就用了各种手段删除,注册表.组策略等等都用上了,却没有丝毫办法.....最后发现的地方特别无语,居然在快捷方式的属性中加上了u ...
- Caffe.proto使用
参考 http://blog.csdn.net/qq_16055159/article/details/45115359 书写.proto文件 作用:编写一个 proto 文件,定义我们程序中需要处理 ...
- WP8 MediaElement 实现循环播放
很简单, 直接在MediaEnded事件里加Play()即可
- Those who are not capable of Control their moods are not supposed to be ready for their baby.
I hate these Stupid Selfish People. We need Children Caring Organization.
- ArrowLayer : A coustom layer animation
Since my other answer (animating two levels of masks) has some graphics glitches, I decided to try r ...
- Redis的简介与安装(windows)
1.简介 Redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串). list(链表).set(集合).zset(sorte ...