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 ...
随机推荐
- 图是否是树 · Graph Valid Tree
[抄题]: 给出 n 个节点,标号分别从 0 到 n - 1 并且给出一个 无向边的列表 (给出每条边的两个顶点), 写一个函数去判断这张`无向`图是否是一棵树. 给出n = 5 并且 edges = ...
- http://classworlds.codehaus.org/apiusage.html
API Usage The Java API can be used to create new realms and connect realms together through importat ...
- DSOframer 的简单介绍和资源整理(2015-09-02重新整理)
DSOframer 是微软提供一款开源的用于在线编辑 Word. Excel .PowerPoint 的 ActiveX 控件.国内很多著名的 OA 中间件,电子印章,签名留痕等大多数是依此改进而来的 ...
- MySQL中触发器
触发器是与某个事件相关的特殊存储过程,与存储过程不同的是,存储过程需要用 call 调用而出发器不需要使用call调用调用. 也就是自己预先定义好了,当某个事件发生时,就会自动出发触发器进行相关的操作 ...
- android studio快捷键大全
----常用快捷键 1.Ctrl+E,可以显示最近编辑的文件列表 2.Shift+Click可以关闭文件 3.Ctrl+[或]可以跳到大括号的开头结尾 4.Ctrl+Shift+Backspace可以 ...
- Mac Sublime Text 浏览器 设置快捷键 让html文件在浏览器打开
首先,进入命令行界面(这里mac和windows一样) 应用命令下载插件 输入命令二 然后就会发现在工具栏多出一个下拉选项 mac的跟它有些区别就是快捷键 mac快捷键是^+√+ V ++字母V,就会 ...
- swoole多进程操作
多个任务同时执行 将顺序执行的任务,转化为并行执行(任务在逻辑上可以并行执行) 比如,我们要对已知的用户数据进行判断,是否需要发送邮件和短信,如果需要发送则发送. 不使用多进程时,我们首先判断是否发送 ...
- [LeetCode 题解]: Reverse Nodes in K-Groups
前言 [LeetCode 题解]系列传送门: http://www.cnblogs.com/double-win/category/573499.html 1.题目描述 Given a li ...
- TFS签入代码时,自动修改工作项的状态为“已解决”
Visual Studio中有一个很酷的功能,就是签入代码到TFS库时,可以关联相应的工作项,实现代码与工作项(需求.任务.Bug等)的关联,从而实现代码的跟踪. 在关联工作项的过程中,如果工作项具备 ...
- C#实现像微信PC版一样的扫码登录功能
现在好些网站都支持扫码登录,感觉上安全了很多,但是本地程序扫码登录的不多,就用C#实现了一下,需要作如下准备 在官网上申请一个企业微信,有条件的话做个企业认证吧,我们的是认证过的,所以账号和本地其他系 ...