问题链接

LeetCode 795

题目解析

给定一个数组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的更多相关文章

  1. 【LeetCode】795. Number of Subarrays with Bounded Maximum 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划 暴力搜索+剪枝 线性遍历 日期 题目地址: ...

  2. 795. Number of Subarrays with Bounded Maximum

    数学的方式 是对于所有的字符分成简单的三类 0 小于 L 1 LR 之间 2 大于R 也就是再求 不包含 2 但是包含1 的子数组个数 不包含2的子数组个数好求 对于连续的相邻的n个 非2类数 就有 ...

  3. [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 ...

  4. 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 ...

  5. [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 ...

  6. C#版 - Leetcode 191. Number of 1 Bits-题解

    版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - L ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 删除链表中的元素 · Remove Linked List Elements

    [抄题]: Remove all elements from a linked list of integers that have value val. ExampleGiven: 1 --> ...

  2. LoadRunner--Analysis各项指标详解

    转载 https://blog.csdn.net/liangfengchang/article/details/45070321 一.常用到的性能测试术语 1.事务(Transaction) 在web ...

  3. chrome扩展安装图文教程

    众所周知chrome的各类插件,扩展很丰富,也有很多经典的应用.但是谷歌经常被墙,无法访问,想要通过访问谷歌的应用市场来直接安装浏览器扩展会比较让人抓狂,好不容易无数次刷新后打开了页面,找到了想要的应 ...

  4. Java设计模式(7)——装饰者模式

    转载:http://blog.csdn.net/yanbober/article/details/45395747 一.装饰者模式的定义 装饰者( Decorator )模式又叫做包装模式.通过一种对 ...

  5. 邹欣,现代软件工程讲义:单元测试&回归测试

    http://www.cnblogs.com/xinz/archive/2011/11/20/2255830.html 邹欣, 现代软件工程讲义 2 开发技术 - 单元测试 & 回归测试

  6. 5 Django-2 的路由层 (URLconf)

    URL 配置 (URLconf) 就像 Django 所支撑网站的目录.它的本质是 URL 与要为该 URL 调用的视图函数之间的映射表:你就是以这种方式告诉 Django,对于客户端发来的某个 UR ...

  7. 三大语言实例 (python,C/C++,Java)

    Python3.5语言实例: #coding = utf-8 import sys def Sub_string(a,b): c=[0]*len(b) for i in range(len(a)): ...

  8. linux每天一小步---awk命令详解

    1 命令功能 awk是linux环境下的一个强大的文本工具,由于awk天生提供对文件中文本分列进行处理,所以如果一个文件中的每行都被特定的分隔符(默认为空格)隔开,我们就可以将这个文件看成是有很多列的 ...

  9. Quartus II 软件生成FFT、NCO、FIR等IP核时卡住不动的解决办法

    据网友表示,遇到这个问题时,在任务管理器中手动关闭quartus_map进程就可以了,由于我的电脑最近一直没有出问题,因此也无法验证.欢迎大家针对这个问题讨论,提出肯定.否定的说法. 另外,很多人表示 ...

  10. SQL描述(2)

    很久之前就想写出来,就是因为自己太懒,憋了怎么久.本文关于使用ORACLE分析函数对一些经济指标进行计算.表indi_value有3个关键的字段:indi_date,indi_value,indi_i ...