原题

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram.

Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3].

The largest rectangle is shown in the shaded area, which has area = 10 unit.

示例

Given heights = [2,1,5,6,2,3],
return 10.

解题思路

第一阶段:

拿到这种题,首先想怎么用最暴力的方法解决这个问题,于是自然而然的想出来枚举的方法,即利用二重循环枚举所有可能的区间,接着再这区间中找出最小的值,然后用(最小值 * 区间宽度)求出每个区间的最大值,最后从这些最大值中找出最大解。

这种解法的时间复杂度是O(n^3),一般都会超时的,那么我们就会想着降低时间复杂度,即去冗余的方法(冗余分为重复计算和不需要计算两种)

第二阶段:

那么哪里存在冗余呢?第一个想到的应该是区间内求最小值时存在重复计算,我们可以使用空间换时间的方法将之前算过的最小值存储下来,这样求最小值[i][j] 就等于MIN( 最小值[i][j-1] ,  j) ,这样我们就时间复杂度降低到了O(n^2)

第三阶段:

通过推演又进一步发现了冗余的地方,比方说对于[9, 8, 10, 3, 12, 11,15]来说,虽然我们枚举了所有可能的区间,计算了其区间内的最小值,但你有没有发现,其中大多数的最小值都是 3, 那么我们能不能改变思路减少该冗余呢?

于是乎我们可以采用枚举最小值的算法,即假设每一个点都是最小值,然后我们计算出其作为最小值可以向左右两边延伸的最大区间(如果该点左边的点值大于该点,则继续往左比较,知道某点比该点值小为止)

最后可以算出每一个点作为最小值所包括的最大面积。面积 = n点高度 * (n右边界 - n左边界 + 1 )

第四阶段:

上一阶段的时间复杂度似乎并没有减少,那么肯定这种新思路引来了新的冗余,这种冗余在哪里呢?

我们发现在找一个点的左右最大区间时存在重复计算,因为如果n点比n - 1点的值大的话,那么n点左边界应该大于等于n - 1点的左边界,于是乎我们可以存储下每个点的左右边界,避免很多重复计算

最终思路:

  1. 枚举所有点,将其作为最小值
  2. 记录每个点的左右边界(计算n点左边界的方法是:判断n是否比n - 1小,如果成立则跳到n - 1 点的左边界x, 比较n是否比x小,如此循环,知道求出左边界)
  3. 枚举每一个点的最大面积,计算最大解

完整代码

public class Solution {
public int largestRectangleArea(int[] heights) {
int n = heights.length;
if (n == 0) {
return 0;
}
// 求左边的边界
int[] left = new int[n];
left[0] = 0;
for (int i=1;i<n;++i) {
int A = i;
while (A > 0 && heights[A - 1] >= heights[i]) {
A = left[A - 1];
}
left[i] = A;
}
// 求右边的边界
int[] right = new int[n];
right[n - 1] = n - 1;
for (int i=n-2;i>=0;--i) {
int A = i;
while (A < n - 1 && heights[A + 1] >= heights[i]) {
A = right[A + 1];
}
right[i] = A;
}
// 枚举每一个最小值的最大面积
int ans = 0;
for (int i=0;i<n;++i) {
ans = Math.max(ans, heights[i] * (right[i] - left[i] + 1));
}
return ans;
}
}

  

    

LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形的更多相关文章

  1. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  2. [leetcode]84. Largest Rectangle in Histogram直方图中的最大矩形

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  3. LeetCode 84. Largest Rectangle in Histogram 单调栈应用

    LeetCode 84. Largest Rectangle in Histogram 单调栈应用 leetcode+ 循环数组,求右边第一个大的数字 求一个数组中右边第一个比他大的数(单调栈 Lee ...

  4. 【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  5. leetCode 84.Largest Rectangle in Histogram (最大矩形直方图) 解题思路和方法

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  6. [leetcode]84.Largest Rectangle in Histogram ,O(n)解法剖析

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  7. [LeetCode#84]Largest Rectangle in Histogram

    Problem: Given n non-negative integers representing the histogram's bar height where the width of ea ...

  8. 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...

  9. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

随机推荐

  1. 基于 Koa平台Node.js开发的KoaHub.js的跳过组件代码

    koahub-skip koahub skip middleware koahub skip Conditionally skip a middleware when a condition is m ...

  2. MSTP多实例的配置

    MSTP多实例的配置 这次实验主要是为了加强对stp生成树协议中,RP(根端口),DP(指定端口),AP(阻塞端口)的判断方法:虽然很多时候不需要我们人工判断,因为当我们吧所有的配置好之后,然后开启生 ...

  3. HTML+CSS-采用DIV+CSS布局的利弊

    单纯的TABLE表格,在浏览器运行上面来说,其效率不如DIV+CSS方式快速,当然,这样也就会影响用的体验. 而且,针对于相同的HTML页面来说TABLE布局,不利于搜索引擎的爬虫爬行下载页面.从而造 ...

  4. CCF 201609-4 交通规划

    问题描述 试题编号: 201609-4 试题名称: 交通规划 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 G国国王来中国参观后,被中国的高速铁路深深的震撼,决定为自己的国家 ...

  5. idea调试SpringMvc, 出现:”通配符的匹配很全面, 但无法找到元素 'mvc:annotation-driven' 的声明“错误的解决方法

    调试json格式输出,出现以下错误: HTTP Status 500 - Servlet.init() for servlet HelloDispatcher threw exception   ty ...

  6. javascript继承---组合式继承

    原型链继承和构造函数继承中存在着很多的缺陷,所以,使用组合式继承了进行弥补这些缺陷 //组合继承 //父函数 function a(){ this.name = "兔子只吃胡萝卜" ...

  7. dos中进入其他盘中的方法

    1.在dos中进入其他盘中直接-->e: 即可,若是进入文件夹中-->cd aaa\bbb 即可. 2.返回上级目录:-->cd..

  8. 爱回收jd图标

    http://jd.aihuishou.com/images/icons.png http://misc.360buyimg.com/201007/skin/df/i/bg_hotsale.gif 来 ...

  9. Jmeter新建用例图示

    添加线程组   添加HTTP请求   编辑HTTP请求 添加HTTP信息头   编辑HTTP信息头 添加断言   添加查看结果树   添加聚合报告   添加响应时间   添加TPS   批量运行命令: ...

  10. Maven项目搭建(三):Maven直接部署项目

    上一章给大家讲解了如何使用Maven搭建SSM框架项目.   这次给大家介绍一下怎么使用Maven直接部署项目.   Maven直接部署项目  1.新建系统变量CATALINA_HOME,值为:Tom ...