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. jdbc.properties 文件的配置

    jdbc.properties文件的配置   使用配置文件访问数据库的优点是: 一次编写随时调用,数据库类型发生变化只需要修改配置文件. 配置文件的设置: 在配置文件中,key-value对应的方式编 ...

  2. java之类的封装

    类和对象成员变量,成员函数特殊的成员变量和成员函数 函数重载 构造函数 静态变量 静态函数面向对象:封装 继承 多态封装 Encapsulation为什么需要封装?外界无法直接操作对象的具体的属性(安 ...

  3. Linux系统的安装(centos的下载地址:http://mirror.symnds.com/distributions/CentOS-vault/6.3/isos/i386/,选择:CentOS-6.3-i386-bin-DVD1.iso 这个下载并进行安装)

    1.首先打开虚拟机: 在上面的那个按钮旁有一个下拉的符号,点开后会看到一个进入固件的按钮,直接点击进去. 便会进入这个界面: 在这个界面其实我们不需要该任何的东西,但是我们需要进入boot界面看一眼, ...

  4. loader的简单使用过程分析

    首先,fragment或者activity必须实现callback接口 必须实现的三个方法为 public Loader<Cursor> onCreateLoader(int id, Bu ...

  5. java 类中 static 的使用

    在类中 static 主要修饰变量,方法及代码块.大致的执行和使用,据个人理解如下: 1.修饰变量: 在修饰变量时,如 ,表示该变量是静态变量,也可称为类变量.当当前变量是静态变量时,该变量被该类的所 ...

  6. KNN算法实现

    import csvimport randomimport mathimport operatorimport numpy as np def loadDataset(filename,split,t ...

  7. 解析IFC数据并转成json格式

    { "com.bim.ifc.ifc2x3.ifc2x3tc1.IfcBuilding (#104)-": [{ "objKey": "GlobalI ...

  8. 3D模型浏览器的实现思路

    前段时间正好浏览了数据结构中关于图的部分,突然就意识到一个问题,3D模型就是用无向图来存储的.仔细想一想是不是这样呢? 一个3D模型去掉材质之后剩下的部分就是点以及点和点之间的连线了,点我们用三维坐标 ...

  9. bzoj4391 [Usaco2015 dec]High Card Low Card

    传送门 分析 神奇的贪心,令f[i]表示前i个每次都出比对方稍微大一点的牌最多能赢几次 g[i]表示从i-n中每次出比对方稍微小一点的牌最多赢几次 ans=max(f[i]+g[i+1]) 0< ...

  10. p4322 [JSOI2016]最佳团体

    传送门 分析 我们不难发现这是一棵树 于是01分数规划然后树上dp即可 代码 #include<iostream> #include<cstdio> #include<c ...