问题链接

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. RemoteExt 远程验证

    public class RemoteExtAttribute : RemoteAttribute { private string _resourceKey; public RemoteExtAtt ...

  2. Shared Libraries with Eclipse on 86_64 (64 bits) systems[z]

    If you followed my previous post http://linuxtortures.blogspot.com/2012/02/shared-libraries-with-ecl ...

  3. Linux gperf命令

    一.简介 GNU 的 gperf 工具是一种 "完美的" 散列函数,可以为用户提供的一组特定字符串生成散列表.散列函数和查找函数的 C/C++ 代码.通过本文学习如何使用 gper ...

  4. 在 macOS 中激活 Astash Professional

    Astah Professional v7.2.0-1ff236版安装完毕后,直接用压缩包内的 astah-pro.jar 替换原安装目录内的同名文件(/Applications/astah prof ...

  5. golang C相互调用带参数

    test.h #ifndef __TEST_H__ #define __TEST_H__ void SetFunc(char* str); extern void InternalFunc(char* ...

  6. swoole多进程操作

    多个任务同时执行 将顺序执行的任务,转化为并行执行(任务在逻辑上可以并行执行) 比如,我们要对已知的用户数据进行判断,是否需要发送邮件和短信,如果需要发送则发送. 不使用多进程时,我们首先判断是否发送 ...

  7. 深刻理解Java编程的7个例子

    1. 阅读下列代码回答问题(第一个Java程序,理解PATH和CLASSPATH,学会使用javac和java命令) package cn.edu.uibe;    public class Hell ...

  8. Websocket出现的错误

    前端使用sockjs,后台使用spring的websocket框架 结果在一个网络较慢的地方,发现tomcat报错信息: Oct 28, 2015 10:10:43 AM org.apache.cat ...

  9. 分析SQL Server Profiler的监控方式

    记得某次给一家公司调优的时候,负责人发给我一堆业务的T-SQL脚本,我面对海量脚本还是从容,虽然不了解内部复杂的业务,但是我们得专注问题的关键 “慢”,我们根据查询的“慢”把他们筛选出来,一一调式优化 ...

  10. ubuntu 安装 hubicfuse

    如果你没有gcc,请先安装gcc: 1: apt-get install build-essential 1. 从github上clone源码: https://github.com/TurboGit ...