74th LeetCode Weekly Contest Number of Subarrays with Bounded Maximum
2024-10-21 14:20:09
We are given an array A
of positive integers, and two positive integers L
and R
(L <= R
).
Return the number of (contiguous, non-empty) subarrays such that the value of the maximum array element in that subarray is at least L
and at most R
.
Example :
Input:
A = [2, 1, 4, 3]
L = 2
R = 3
Output: 3
Explanation: There are three subarrays that meet the requirements: [2], [2, 1], [3].
Note:
- L, R and
A[i]
will be an integer in the range[0, 10^9]
. - The length of
A
will be in the range of[1, 50000]
.
有多少连续的子数组,必须存在[L,R],但不能超过R
应该知道了,A[i]在L,R之间是num+=(i-j),超过R需要记录j=i,但如何处理小于L的数字
因为必须存在[L,R],单纯的小于L不能算,那么就拿一个k记录一下不在[L,R]的位置,然后(i-j)-(i-k+1)就好了
class Solution {
public:
int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
int aLen=A.size();
int num=;
int k=;
int i=,j=-;
for(int i=;i<aLen;i++){
if(A[i]>R){
k=i+;
j=i;
}else if(A[i]<L){
num+=(i-j)-(i-k+);
}else{
num+=(i-j);
k=i+;
}
}
return num;
}
};
最新文章
- jsp中头的导入两种方式区别
- WPF 窗口自适应
- 不使用spring的情况下原生java代码两种方式操作mongodb数据库
- Hibernate学习笔记--------2.一多|多多的CRUD
- LTTng调试: 一个系统软件工程师的随手涂鸦
- HDU 1573 X问题 中国剩余定理
- JS页面延迟执行一些方法(整理)
- 贝叶斯A/B测试 - 一种计算两种概率分布差异性的方法过程
- tnsping 不通
- 【python】——购物车
- C#编程(六十五)----------表达式树
- SharePoint 数据库管理-PowerShell
- JS--数组和字典
- 镣铐之舞:美团安全工程师Black Hat USA演讲
- POJ - Problem 1275 - Cashier Employment
- NVIDIA GTC照片
- CSS3制作各种形状图像(转)
- select展开时 重新加载 option,ie 折叠问题 以及 chrome 没有变化问题
- JS知识总结
- 神奇的Timer