问题链接

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. 8-matlab-gui-显示图片有坐标刻度问题

    在图片上显示图片时,总是有图片,一遍做法是使得刻度为空就可了: 在你的每一个axes的CreateFcn函数中添加一下代码即可:set(hObject,'xTick',[]);set(hObject, ...

  2. [z]hadoop生态系统

    http://developer.51cto.com/art/201311/415639_all.htm

  3. fiddler手机抓包

    配置好之后,发现手机不能连接代理服务器,用netstat发现根本就没有手机的ip地址,于是猜想是不是防火墙的问题,关闭防火墙果然能连上,开启防火墙又连不上了,这就说明确实是防火墙的问题,关闭防火墙又很 ...

  4. py学习之FTP

    1.FTP之参数解析与命令分发 a) 层级目录如下 b) 配置文件如下 #!/usr/bin/env python # -*- coding:utf8 -*- import socket sk=soc ...

  5. pyspider示例代码二:解析JSON数据

    本系列文章主要记录和讲解pyspider的示例代码,希望能抛砖引玉.pyspider示例代码官方网站是http://demo.pyspider.org/.上面的示例代码太多,无从下手.因此本人找出一下 ...

  6. jquery怎么根据后台传过来的值动态设置下拉框、单选框选中

    $(function(){ var sex=$("#sex").val(); var marriageStatus=$("#marriageStatus").v ...

  7. CodeForces 688A Opponents (水题)

    题意:给定 n 行数,让你找出连续最多的全是1的个数. 析:好像也没什么可说的,那就判断一下,并不断更新最大值呗. 代码如下: #include <iostream> #include & ...

  8. UVa 1614 Hell on the Markets (贪心+推理)

    题意:给定一个长度为 n 的序列,满足 1 <= ai <= i,要求确实每一个的符号,使得它们和为0. 析:首先这一个贪心的题目,再首先不是我想出来的,是我猜的,但并不知道为什么,然后在 ...

  9. Set List Map

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  10. 关于windows服务注册的问题

    开发工具:VS2012 语言:C# 今天的工作内容是把wcf服务以windows服务的方式运行,由于之前没有做过windows服务,所有在网上找了些文章来看下,发现创建windows 服务是一件很简单 ...