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.

  1. Example :
  2. Input:
  3. A = [2, 1, 4, 3]
  4. L = 2
  5. R = 3
  6. Output: 3
  7. 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,表示一个区间范围,让我们求有多少个这样的子数组,使得其最大值在[L, R]区间的范围内。既然是求子数组的问题,那么最直接,最暴力的方法就是遍历所有的子数组,然后维护一个当前的最大值,只要这个最大值在[L, R]区间的范围内,结果res自增1即可。但是这种最原始,最粗犷的暴力搜索法,OJ不答应。但是其实我们略作优化,就可以通过了。优化的方法是,首先,如果当前数字大于R了,那么其实后面就不用再遍历了,不管当前这个数字是不是最大值,它都已经大于R了,那么最大值可能会更大,所以没有必要再继续遍历下去了。同样的剪枝也要加在内层循环中加,当curMax大于R时,直接break掉内层循环即可,参见代码如下:

解法一:

  1. class Solution {
  2. public:
  3. int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
  4. int res = , n = A.size();
  5. for (int i = ; i < n; ++i) {
  6. if (A[i] > R) continue;
  7. int curMax = INT_MIN;
  8. for (int j = i; j < n; ++j) {
  9. curMax = max(curMax, A[j]);
  10. if (curMax > R) break;
  11. if (curMax >= L) ++res;
  12. }
  13. }
  14. return res;
  15. }
  16. };

虽然上面的方法做了剪枝后能通过OJ,但是我们能不能在线性的时间复杂度内完成呢。答案是肯定的,我们先来看一种官方解答贴中的方法,这种方法是用一个子函数来算出最大值在[-∞, x]范围内的子数组的个数,而这种区间只需要一个循环就够了,为啥呢?我们来看数组[2, 1, 4, 3]的最大值在[-∞, 4]范围内的子数组的个数。当遍历到2时,只有一个子数组[2],遍历到1时,有三个子数组,[2], [1], [2,1]。当遍历到4时,有六个子数组,[2], [1], [4], [2,1], [1,4], [2,1,4]。当遍历到3时,有十个子数组。其实如果长度为n的数组的最大值在范围[-∞, x]内的话,其所有子数组都是符合题意的,而长度为n的数组共有n(n+1)/2个子数组,刚好是等差数列的求和公式。所以我们在遍历数组的时候,如果当前数组小于等于x,则cur自增1,然后将cur加到结果res中;如果大于x,则cur重置为0。这样我们可以正确求出最大值在[-∞, x]范围内的子数组的个数。而要求最大值在[L, R]范围内的子数组的个数,只需要用最大值在[-∞, R]范围内的子数组的个数,减去最大值在[-∞, L-1]范围内的子数组的个数即可,参见代码如下:

解法二:

  1. class Solution {
  2. public:
  3. int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
  4. return count(A, R) - count(A, L - );
  5. }
  6. int count(vector<int>& A, int bound) {
  7. int res = , cur = ;
  8. for (int x : A) {
  9. cur = (x <= bound) ? cur + : ;
  10. res += cur;
  11. }
  12. return res;
  13. }
  14. };

下面这种解法也是线性时间复杂度的,跟上面解法的原理很类似,只不过没有写子函数而已。我们使用left和right来分别标记子数组的左右边界,使得其最大值在范围[L,R]内。那么当遍历到的数字大于等于L时,right赋值为当前位置i,那么每次res加上right - left,随着right的不停自增1,每次加上的right - left,实际上也是一个等差数列,跟上面解法中的子函数本质时一样的。当A[i]大于R的时候,left = i,那么此时A[i]肯定也大于等于L,于是rihgt=i,那么right - left为0,相当于上面的cur重置为0的操作,发现本质联系了吧,参见代码如下:

解法三:

  1. class Solution {
  2. public:
  3. int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
  4. int res = , left = -, right = -;
  5. for (int i = ; i < A.size(); ++i) {
  6. if (A[i] > R) left = i;
  7. if (A[i] >= L) right = i;
  8. res += right - left;
  9. }
  10. return res;
  11. }
  12. };

我们可以对上面的解法稍稍做下优化,在A[i] > R的时候,left和right都赋值为i,然后continue,这样省去了后面的用0来更新结果res的步骤,能提高一些运行效率,参见代码如下:

解法四:

  1. class Solution {
  2. public:
  3. int numSubarrayBoundedMax(vector<int>& A, int L, int R) {
  4. int res = , left = -, right = -;
  5. for (int i = ; i < A.size(); ++i) {
  6. if (A[i] > R) {
  7. left = right = i;
  8. continue;
  9. }
  10. if (A[i] >= L) right = i;
  11. res += right - left;
  12. }
  13. return res;
  14. }
  15. };

参考资料:

https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/solution/

https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/discuss/117585/Java-9-liner

https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/discuss/117616/C++-O(n)-less10-lines

https://leetcode.com/problems/number-of-subarrays-with-bounded-maximum/discuss/119162/Clean-and-simple-O(n)-Java

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Number of Subarrays with Bounded Maximum 有界限最大值的子数组数量的更多相关文章

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

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

  2. LeetCode 795. Number of Subarrays with Bounded Maximum

    问题链接 LeetCode 795 题目解析 给定一个数组A,左右范围L.R.求子数组的数量,要求:子数组最大值在L.R之间. 解题思路 子数组必须连续,利用最大值R对数组进行分段,设定变量 left ...

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

  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. 795. Number of Subarrays with Bounded Maximum

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

  6. 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

    Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containin ...

  7. [LeetCode] 918. Maximum Sum Circular Subarray 环形子数组的最大和

    Given a circular array C of integers represented by A, find the maximum possible sum of a non-empty ...

  8. LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$

    Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies fol ...

  9. 152. Maximum Product Subarray最大乘积子数组/是否连续

    [抄题]: Given an integer array nums, find the contiguous subarray within an array (containing at least ...

随机推荐

  1. DirectX11 With Windows SDK--12 深度/模板状态、平面镜反射绘制

    前言 深度/模板测试使用的是与后备缓冲区同等分辨率大小的缓冲区,每个元素的一部分连续位用于深度测试,其余的则用作模板测试.两个测试的目的都是为了能够根据深度/模板状态需求的设置来选择需要绘制的像素. ...

  2. 第四节:框架前期准备篇之进程外Session的两种配置方式

    一. 基本介绍 1. 背景:Asp.Net默认的Session机制是进程内,存储在服务器端内存中,有这么几个缺点: ①:既然存在内存中,空间有限,不能存储大数据量信息,数据量多的话Session会被挤 ...

  3. JAVA进阶4

    间歇性混吃等死,持续性踌躇满志系列-------------第4天 1.静态内部类求极值 class MaxMin{ public static class Result{ //表示最大值.最小值 p ...

  4. springMVC中controller的几种返回类型

    ==网文1,还不错,感觉比较老旧springMVC中controller的几种返回类型 - CSDN博客http://blog.csdn.net/qq_16071145/article/details ...

  5. python学习第25天

    异常处理 什么是异常?什么是错误? 1,程序中难免出现错误. 错误主要分为两种: 1,语法错误 语法错误是根本上的错误,无法通过PYTHON解释器.完全无法执行,是在程序中不应该出现的错误.无法进行异 ...

  6. java----dom4j 解析XML

    dom4j: 由于内部采用迭代器,适合读取大文档: 数据块 1.下载 https://dom4j.github.io/ 2.添加包到工程目录下 使用 import org.dom4j.Document ...

  7. 一次ES故障排查过程

    作者:莫那鲁道 原文:http://thinkinjava.cn/#blog 某天晚上,某环境 ES 出现阻塞, 运行缓慢.于是开始排查问题的过程. 开始 思路:现象是阻塞,通常是 CPU 彪高,导致 ...

  8. windows无法安装msi文件

    命令提示符(管理员身份运行): 输入:msiexec /i e:\spark\scala-2.11.12.msi 其中e:\spark\scala-2.11.12.msi:就是安装文件的位置.

  9. entityframework单例模式泛型用法

    public class yms_Entity<T> where T :DbContext { private static T _instance; public static read ...

  10. json2mysql

    import json import psycopg2 import sys conn = psycopg2.connect(dbname='dev', host='127.0.0.1', port= ...