当前位置:网站首页>2302. Count the number of subarrays with a score less than k - sliding array - double hundred code
2302. Count the number of subarrays with a score less than k - sliding array - double hundred code
2022-07-23 09:12:00 【Mr Gao】
2302. The statistical score is less than K Number of subarrays of
A digital fraction Defined as the sum of arrays multiply Length of array .
For example ,[1, 2, 3, 4, 5] The score of is (1 + 2 + 3 + 4 + 5) * 5 = 75 .
Here's an array of positive integers nums And an integer k , Please return nums Middle score Strictly less than k Of Number of non empty integer subarrays .
Subarray Is a sequence of consecutive elements in an array .
Example 1:
Input :nums = [2,1,4,3,5], k = 10
Output :6
explain :
Yes 6 The score of the subarray is less than 10 :
- [2] The score is 2 * 1 = 2 .
- [1] The score is 1 * 1 = 1 .
- [4] The score is 4 * 1 = 4 .
- [3] The score is 3 * 1 = 3 .
- [5] The score is 5 * 1 = 5 .
- [2,1] The score is (2 + 1) * 2 = 6 .
Be careful , Subarray [1,4] and [4,3,5] Unqualified , Because their scores are 10 and 36, But we need to find that the score of the subarray is strictly less than 10 .
Example 2:
Input :nums = [1,1,1], k = 5
Output :5
explain :
except [1,1,1] The score of each sub array is less than 5 .
[1,1,1] The score is (1 + 1 + 1) * 3 = 9 , Greater than 5 .
So there are 5 The subarray score is less than 5 .
The solution code is as follows :
long long countSubarrays(int* nums, int numsSize, long long k){
long long count=0;
long long sum=0,i,j,low=0,high=0;
long long arrc=0;
while(low<numsSize){
// printf("--lh %d %d",high,low);
if(high<numsSize){
while((sum+nums[high])*(high-low+1)<k){
sum=sum+nums[high];
high++;
if(high>numsSize-1){
break;
}
}
}
count=count+high-low;
sum=sum-nums[low];
low++;
}
return count;
}
The blogger also wrote a violent Algorithm , It can also solve problems , But the event cost is a little higher :
long long countSubarrays(int* nums, int numsSize, long long k){
long long count=0;
long long sum=0,i,j,low=0,high=0;
long long arrc=0;
for(i=0;i<numsSize;i++){
sum=nums[i];
arrc=1;
if(sum<k){
count++;
}
else{
continue;
}
for(j=i+1;j<numsSize;j++){
sum=sum+nums[j];
arrc++;
if(sum*arrc<k){
count++;
}
else{
break;
}
}
}
return count;
}
边栏推荐
- UGUI源码解析——IMaskable
- Jmeter---Jmeter安装教程
- Practical exercise | a simple method for MySQL processlist table and Navicat monitor to identify slow queries
- XSS labs customs collection
- LiveQing直播RTMP点播视频流媒体平台如何携带登录接口返回的sid和token接口调用鉴权streamToken视频流鉴权
- DALSA smart camera boa spot communicates with Siemens s7-1200 PROFINET
- [cloud native] in the era of storm and cloud, the sharp edge of DBAs is out of the sheath
- 数论 —— 整除分块,常见经典例题。
- 基于SSM的博客系统【带后台管理】
- Airserver third party projection software v7.3.0 Chinese Version (airplay terminal utility)
猜你喜欢

DOM series prohibit selected text and prohibit right-click menu

Template school jumpserver security operation and maintenance audit screen

Mathematical modeling -- graph and network models and methods (II)
![[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code](/img/df/f29eed667c2a7dc02d93ac3f424198.png)
[concurrent programming] Chapter 2: go deep into the reentrantlock lock lock from the core source code

50道经典计算机网络面试题,你答得上几个?(上)

砥砺前行新征程,城链科技狂欢庆典在厦门隆重举行

解析steam与创客教育课堂的统筹规划

IDM下载器免费高质量的Win下载工具无使用限制

Summary of some open source libraries that drive MCU hardware debugger (including stlink debugger)

超全PMP备考文档汇总
随机推荐
JMeter --- JMeter installation tutorial
Unity3d learning note 9 - loading textures
Mathematical modeling interpolation fitting
2000. 反转单词前缀
关系表达式 大于> 小于< 全等=== Nan isNan() 逻辑运算符 双感叹号!! && || % ++ -- 短路计算 赋值表达式 快捷运算符 顺序 闰年
rust allow dead_code
程序员不会 jvm?骨灰级工程师:全等着被淘汰吧!这是必会技能!
数论 —— 整除分块,常见经典例题。
在线抠图和换背景及擦除工具
基于共轭梯度法的对抗攻击
博途PLC信号处理系列之限幅消抖滤波
Day3 POC and exp learning Pikachu brute force cracking with token
解读随着教育改革的深入steam教育
十. 实战——云服务器
解析创客教育活动所需的空间实践场
DOM系列之禁止选中文字和禁止右键菜单
Flutter 3.0
提升从改变开始...
Practical exercise | a simple method for MySQL processlist table and Navicat monitor to identify slow queries
疫情隔离区订餐系统的开发