LeetCode 795. Number of Subarrays with Bounded Maximum
问题链接
题目解析
给定一个数组A,左右范围L、R。求子数组的数量,要求:子数组最大值在L、R之间。
解题思路
子数组必须连续,利用最大值R对数组进行分段,设定变量 left 记录每段开始位置(\(A[left] > R\),\(A[left+1] ≤ R\)),初始值为-1。
遍历数组,通过A[i]大小判断:
- 若 A[i] 在L、R之间,则 ans+=(i-j);
- 若 A[i] 大于R,则更改左界 left=i;
- 关键在于处理小于L的情况,由于需要子数组的最大值在L、R之间,单纯的 A[i] 肯定不能算,需要知道最近合法的数字,即右界。设定变量 right 记录合法的数字的右界(\(L ≤ A[right] ≤ R\)),当然,在A[i]大于R时,左界left和右界right都设置为i。这种情况下,ans += (i-left)-(i-right)。
参考代码
class Solution {
public:
int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
int len = A.size();
int ans = 0;
int left = -1;//左界
int right = -1;//最近合法位置
for(int i=0; i<len; i++) {
if(A[i] > R) {
left = i;
right = i;
}
else if(A[i]<L) {
ans += (i-left)-(i-right);
}
else {
ans += (i-left);
right = i;
}
}
return ans;
}
};
LeetCode All in One题解汇总(持续更新中...)
本文版权归作者AlvinZH和博客园所有,欢迎转载和商用,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
LeetCode 795. Number of Subarrays with Bounded Maximum的更多相关文章
- 【LeetCode】795. Number of Subarrays with Bounded Maximum 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 暴力搜索+剪枝 线性遍历 日期 题目地址: ...
- 795. Number of Subarrays with Bounded Maximum
数学的方式 是对于所有的字符分成简单的三类 0 小于 L 1 LR 之间 2 大于R 也就是再求 不包含 2 但是包含1 的子数组个数 不包含2的子数组个数好求 对于连续的相邻的n个 非2类数 就有 ...
- [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 ...
- 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 ...
- [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 ...
- C#版 - Leetcode 191. Number of 1 Bits-题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...
- [leetcode]200. Number of Islands岛屿个数
Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surro ...
- [leetcode]694. Number of Distinct Islands你究竟有几个异小岛?
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
- [LeetCode] 711. Number of Distinct Islands II_hard tag: DFS
Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...
随机推荐
- 界面编程与视图(View)组件
1.视图组件与容器组件 Android应用绝大部分UI组件都放在Android.widget包及其子包.android.view包及其子包中,其所有UI组件都继承了view类,view组件代表一个空白 ...
- 避免Block中的强引用环
[避免Block中的强引用环] In manual reference counting mode, __block id x; has the effect of not retaining x. ...
- maven scope简单说明
compile:默认的scope.任何定义在compile scope下的依赖将会在所有的class paths下可用.maven工程会将其打包到最终的arifact中.如果你构建一个WAR类型的ar ...
- sqlserver2012 清除日志
1. backup log wwgl_demo to disk='D:\DATA_BACKUP\2017-07-19.log' 2. 右键数据库-->任务-->收缩-->文件 ...
- IntelliJ IDEA 2017版 SpringBoot徒手书写HelloWorld
1.打开编译器,选择File---->New---->Project 2.弹出设置界面,选择如图样式的1.2.3 3.设置包名称 4.继续next 5.finish完成即可 6.自动生成目 ...
- BZOJ 4326 NOIP2015 运输计划 (二分+树上差分)
4326: NOIP2015 运输计划 Time Limit: 30 Sec Memory Limit: 128 MBSubmit: 1930 Solved: 1231[Submit][Statu ...
- 与table有关的布局
当IE8发布时,它将支持很多新的CSS display属性值,包括与表格相关的属性值:table.table-row和table-cell,它也是最后一款支持这些属性值的主流浏览器.它标志着复杂CSS ...
- Shell编程-04-Shell中变量数值计算
目录 算术运算符 算术运算命令 数值运算用法 算术运算符 在任何一门形式的语言中均会存在算术运算的情况,Shell常见的运算符如下所示: 运算符 含义 + - * / % 加 减 乘 除 求余 ...
- Java反射API研究(3)——java.lang.Class<T>
对于反射来说,Class是核心,任何反射的对象都需要通过Class来获得. Class 类的实例表示正在运行的 Java 应用程序中的类和接口.枚举是一种类,注释是一种接口.每个数组属于被映射为 Cl ...
- lambda distinct
public ActionResult Index() { IList<RegisterModel> regList = new List<RegisterModel>() { ...