74th LeetCode Weekly Contest 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 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;
}
};
74th LeetCode Weekly Contest Number of Subarrays with Bounded Maximum的更多相关文章
- 【LeetCode】795. Number of Subarrays with Bounded Maximum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 暴力搜索+剪枝 线性遍历 日期 题目地址: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- [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 ...
- LeetCode 795. Number of Subarrays with Bounded Maximum
问题链接 LeetCode 795 题目解析 给定一个数组A,左右范围L.R.求子数组的数量,要求:子数组最大值在L.R之间. 解题思路 子数组必须连续,利用最大值R对数组进行分段,设定变量 left ...
- [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 ...
- 795. Number of Subarrays with Bounded Maximum
数学的方式 是对于所有的字符分成简单的三类 0 小于 L 1 LR 之间 2 大于R 也就是再求 不包含 2 但是包含1 的子数组个数 不包含2的子数组个数好求 对于连续的相邻的n个 非2类数 就有 ...
随机推荐
- Android禁止程序自动旋转的配置
在想要禁止的Activity中加入 android:screenOrientation="portrait" 属性,其中,portrait是竖屏,landscape是横屏
- Windows版本Apache+php的Xhprof应用
[知识] {Apache} Apache是世界使用排名第一的Web服务器软件.它可以运行在几乎所有广泛使用的计算机平台上,由于其跨平台和安全性被广泛使用,是最流行的Web服务器端软件之一. {PHP} ...
- [转]SQL 模糊查询
在进行数据库查询时,有完整查询和模糊查询之分. 一般模糊查询语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,% :表 ...
- js转化与排序
1.对象转化为数组 object.keys() var obj={a:3,b:7,c:8,d:false} alert(Object.keys(obj)) 注意此函数会把对象的key转化为数组 spl ...
- RTX这种东西究竟有什么价值?
我在第一家公司工作的时候,同事沟通用的就是RTX,第一感觉就是这么简单的软件也能卖钱? 这种东西有啥价值啊?不就是个没广告蓝色UI的qq吗? 还是那句话,当你已经习惯了一个东西之后,你不会感觉到他的价 ...
- A - Dictionary
传送门 题目大意 给你n个字符串,问是否可以通过改变26个字母的排列顺序是这n个字符串的字典序是非降排列的. 分析 我们考虑设相邻两个字符串的第一个不相同字符的位置为j,以为要求字典序不降,所以有第i ...
- vue 之 计算属性和侦听器
计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div> {{ message.split('').rever ...
- 《Effective Java》第5章 泛型
第23条:请不要在新代码中使用原生态类型 声明中具有一个或者多个类型参数( type parameter)的类或者接口,就是泛型(generic)类或者接口. 每种泛型定义一组参数化的类型(param ...
- tftp命令详解
TFTP协议简介TFTP是用来下载远程文件的最简单网络协议,它其于UDP协议而实现.嵌入式linux的tftp开发环境包括两个方面: 一是linux服务器端的tftp-server支持,二是嵌入式目标 ...
- ubuntu - 官方简体中文wiki,有你想要的ubuntu的一切
ubuntu官方简体中文wiki:http://wiki.ubuntu.org.cn/%E9%A6%96%E9%A1%B5