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. android wifi框架

    ---恢复内容开始--- frameworks/base/services/java/com/android/server/wifi 中的ReadMe文件 WifiService: Implement ...

  2. 【转】手把手教你用Strace诊断问题

    原博客地址:http://huoding.com/2015/10/16/474 早些年,如果你知道有个 strace 命令,就很牛了,而现在大家基本都知道 strace 了,如果你遇到性能问题求助别人 ...

  3. 100741A Queries

    传送门 题目 Mathematicians are interesting (sometimes, I would say, even crazy) people. For example, my f ...

  4. enumerate()函数

    for index,value in enumerate(list):       print index,value 等于for i in range(0,len(list)): print i,l ...

  5. SDKD 2017 Summer Single Training #03

    今天的题目有 6 个. 第一题: CodeForces - 400D  Dima and Bacteria 这个题实际是不难的,难的可能在题意的理解上还有题干有点长,这个题很考察题意上面,知识点很熟悉 ...

  6. String的split(String regex, int limit)方法小结

    split(String regex, int limit)方法,头一个参数String regex表示字符串分割的模式,包括分隔符和正则表达式:但是第二个参数limit比较迷糊人,api中这样解释: ...

  7. css 隐藏html元素的方法

    1.常见方法 display:none 这个方法的问题是 元素被隐藏了 同时该元素不占位置了,应该也可以说该元素没有高度和宽度了 2.常见方法 visibility: hidden; 这个方法和dis ...

  8. HttpWebRequest,HttpWebResponse 使用

    目的:工作中已经两次使用了,特此记录一下,并写好注释 /// <summary> /// HttpWebRequest的基本配置 /// </summary> public c ...

  9. sql 插入

    今天处理了一个有关数据库表数据批量插入的问题.部分细节,自己之前没有遇到过.索性就整理下来,做个备忘录. 主要是将一个表的数据导入到另一张表中.这种插入方法,需注意两张表的对于字段的数据结构需要保持一 ...

  10. docker初探

    1.什么是docker: 可以理解为一个可移植的集装箱容器,开发者可以打包他们的应用以及依赖包到一个可移植的容器中. 2.docker安装及使用(ubuntu16.04) (1)首先通过apt-get ...