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

74th LeetCode Weekly Contest Number of Subarrays with Bounded Maximum的更多相关文章

  1. 【LeetCode】795. Number of Subarrays with Bounded Maximum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 暴力搜索+剪枝 线性遍历 日期 题目地址: ...

  2. 74th LeetCode Weekly Contest Valid Number of Matching Subsequences

    Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of ...

  3. 109th LeetCode Weekly Contest Number of Recent Calls

    Write a class RecentCounter to count recent requests. It has only one method: ping(int t), where t r ...

  4. 74th LeetCode Weekly Contest Preimage Size of Factorial Zeroes Function

    Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by con ...

  5. 74th LeetCode Weekly Contest Valid Tic-Tac-Toe State

    A Tic-Tac-Toe board is given as a string array board. Return True if and only if it is possible to r ...

  6. [LeetCode] Number of Subarrays with Bounded Maximum 有界限最大值的子数组数量

    We are given an array A of positive integers, and two positive integers L and R (L <= R). Return ...

  7. LeetCode 795. Number of Subarrays with Bounded Maximum

    问题链接 LeetCode 795 题目解析 给定一个数组A,左右范围L.R.求子数组的数量,要求:子数组最大值在L.R之间. 解题思路 子数组必须连续,利用最大值R对数组进行分段,设定变量 left ...

  8. [Swift]LeetCode795. 区间子数组个数 | Number of Subarrays with Bounded Maximum

    We are given an array A of positive integers, and two positive integers L and R (L <= R). Return ...

  9. 795. Number of Subarrays with Bounded Maximum

    数学的方式 是对于所有的字符分成简单的三类 0 小于 L 1 LR 之间 2 大于R 也就是再求 不包含 2 但是包含1 的子数组个数 不包含2的子数组个数好求 对于连续的相邻的n个 非2类数 就有 ...

随机推荐

  1. dp-最小点对问题

    dp-最小点对问题 //最小点对问题 //采用分治思想,先分成两个子集分别求出最短距离d //再对两个子集进行合并,在一个dx2d的矩形中,最多可能有6个点距离小于d //按y排序,当x增长时求出这6 ...

  2. Using JConsole

    Using JConsole 转自 https://docs.oracle.com/javase/8/docs/technotes/guides/management/jconsole.html Th ...

  3. HTML5 学习指导

    HTML 语义 HTML5为我们提供了很多旨在精确描述内容的语义元素.确保你可以从它丰富的词汇中获益. <!-- bad --> <div id="main"&g ...

  4. REST API (更新文档)

    Elasticsearch的更新文档API准许通过脚本操作来更新文档.更新操作从索引中获取文档,执行脚本,然后获得返回结果.它使用版本号来控制文档获取或者重建索引. 我们新建一个文档: 请求:PUT  ...

  5. [poj2398]Toy Storage

    接替关键:和上题类似,输出不同,注意输入这道题需要排序. #include<cstdio> #include<cstring> #include<algorithm> ...

  6. SVN资源库报错:Could not create the view: org.tigris.subversion.subclipse.ui.repository.RepositoriesView

    解决方法: 关闭正在运行的myeclipse,然后打开myeclipse安装路径(我的安装在c盘): c:\ProgramFiles\MyEclipse\MyEclipse Professional ...

  7. java开发中用到的技术(持续更新.....)

    一.数据库 1.数据库连接池:当jdbc连接数据库使用DriverManager 获取时,每次向数据库建立连接的时候都要讲connection加载到内存中,当同时使用的用户数量较大时,会造成服务器不堪 ...

  8. EZOJ #82

    传送门 分析 首先我们发现$k$位数实际就是一位的情况的$k$次方 考虑一开始的总方案数是$2^{nm}$ 我们每一次枚举其中有$i$行$j$列 对于这种情况的容斥系数为$(-1)^{i+j}$ 方案 ...

  9. hdu6357 Hills And Valleys

    传送门 题目大意 给定一个序列A,求翻转A中一个区间之后的最长不降子序列的长度即翻转的区间 分析 发现直接枚举翻转的区间的话是无论如何都不行的,于是有一个非常神奇的做法.我们再设一个序列B = {0, ...

  10. unix 下 shell 遍历指定范围内的日期

    UNIX下遍历日期,date 没有 -d 的参数,所以需要自己处理. 下面使用时差的方法进行计算,遍历的日期是降序的 #!/usr/bin/ksh . $HOME/.profile timelag= ...