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 Land 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;
}
};

最新文章

  1. jsp中头的导入两种方式区别
  2. WPF 窗口自适应
  3. 不使用spring的情况下原生java代码两种方式操作mongodb数据库
  4. Hibernate学习笔记--------2.一多|多多的CRUD
  5. LTTng调试: 一个系统软件工程师的随手涂鸦
  6. HDU 1573 X问题 中国剩余定理
  7. JS页面延迟执行一些方法(整理)
  8. 贝叶斯A/B测试 - 一种计算两种概率分布差异性的方法过程
  9. tnsping 不通
  10. 【python】——购物车
  11. C#编程(六十五)----------表达式树
  12. SharePoint 数据库管理-PowerShell
  13. JS--数组和字典
  14. 镣铐之舞:美团安全工程师Black Hat USA演讲
  15. POJ - Problem 1275 - Cashier Employment
  16. NVIDIA GTC照片
  17. CSS3制作各种形状图像(转)
  18. select展开时 重新加载 option,ie 折叠问题 以及 chrome 没有变化问题
  19. JS知识总结
  20. 神奇的Timer

热门文章

  1. taglib标签在web.xml文件中报错的解决办法
  2. win10获取超级管理员权限脚本实现
  3. idea 修改Recent projects
  4. 202. Happy Number 平方循环数
  5. ZROI2018普转提day2t1
  6. Entity Framework Tutorial Basics(3):Entity Framework Architecture
  7. 总结:kathasis如何发送get请求获取数据
  8. 删除GHOST中win7桌面IE删不掉的解决办法
  9. PreparedStatement预编译对象实现
  10. Spring 特点