题目:

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. (Hard)

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.

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

分析:

非常经典的一道题目,想了好久也没想出来,还是参考了讨论区。

题意是找到最大的直方图面积。显而易见的思路是对于每个高度heights[i],向左向右找到第一个小于它的位置,即为以此高度能组成的最大直方图面积。

循环下来求得所有高度为底线的最大直方图面积,求max即可。

但寻找左右边界的时间复杂度O(n),遍历的时间复杂度也是O(n), O(n^2)的算法是超时的。

中间自己还想到了一些简单的优化想法,比如空间换时间,记录求过的边界并利用,但在最坏样例下还是O(n^2)的,还是不行。

题目的解法是采用一个栈,非常的巧妙。

维护一个栈;

当遍历到位置的高度大于栈顶高度时: 压栈,同时说明了,此时这个位置的前一位的下标即为这个高度的左边界;(满足向左数第一个小于他的)

当遍历到位置的高度小于栈顶高度时:一直弹栈至上述条件不满足,同时对每一个弹出的元素,他的右边界即为当前遍历到的位置(右数第一个大于他的)。

此时可以根据左右边界计算以此高度能组成的最大直方图面积。

可以直接在栈中存储下标,便于计算边界差值(左边界是栈内部的下一个元素,因为只要能压栈,说明前一个即为其左边界),同时也方便取出某一下标的高度。

注意:栈为空时,下标取-1;

代码:

 1 class Solution {
 2 public:
 3     int largestRectangleArea(vector<int>& heights) {
 4         int result = 0;
 5         int sz = heights.size();
 6         stack<int> s;
 7         for (int i = 0; i < sz; ++i) {
 8             if (s.empty() || heights[i] >= heights[s.top()]) {
 9                 s.push(i);
10             }
11             else {
12                 while (!s.empty() && heights[s.top()] > heights[i]) {
13                     int cur = s.top();
14                     s.pop();
15                     int prev = (s.empty()) ? -1 : s.top();
16                     result = max(result, (i - prev - 1) * heights[cur] );
17                 }
18                 s.push(i);
19             }
20         }
21         while (!s.empty()) {
22             int cur = s.top();
23             s.pop();
24             int prev = (s.empty()) ? -1 : s.top();
25             result = max(result, (sz - prev - 1) * heights[cur] );
26         }
27         return result;
28     }
29 };

转载于:https://www.cnblogs.com/wangxiaobao/p/5958411.html

LeetCode84 Largest Rectangle in Histogram相关推荐

  1. LeetCode 84. Largest Rectangle in Histogram

    LeetCode 84. Largest Rectangle in Histogram Solution1:我的答案 循环里头套了一个动态规划,缺点是当heights个数或最大高度多高时会很耗时间!! ...

  2. [leetcode]Largest Rectangle in Histogram @ Python

    原题地址:https://oj.leetcode.com/problems/largest-rectangle-in-histogram/ 题意: Given n non-negative integ ...

  3. [leetcode]84. Largest Rectangle in Histogram c语言

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

  4. 84直方图最大矩形覆盖 · Largest Rectangle in Histogram

    [抄题]: Given n non-negative integers representing the histogram's bar height where the width of each ...

  5. leetcode: Largest Rectangle in Histogram,Maximal Square,Maximal Square问题

    Largest Rectangle问题 题目描述 Given n non-negative integers representing the histogram's bar height where ...

  6. 84. Largest Rectangle in Histogram

    Title 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定 ...

  7. leetcode 84. Largest Rectangle in Histogram | 84. 柱状图中最大的矩形(单调栈)

    题目 https://leetcode.com/problems/largest-rectangle-in-histogram/ 题解 一句话总结:遍历数组,对于每个height[i],以其自身的高度 ...

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

    题目描述: 给定 n 个非负整数,用来表示柱状图中各个柱子的高度.每个柱子彼此相邻,且宽度为 1 . 求在该柱状图中,能够勾勒出来的矩形的最大面积. 以上是柱状图的示例,其中每个柱子的宽度为 1,给定 ...

  9. LeetCode hard 84. Largest Rectangle in Histogram--python,java 15行,c++ 15行 解法

    题目地址: Given n non-negative integers representing the histogram's bar height where the width of each ...

最新文章

  1. 索尼因需量大增而增加PSVR产量
  2. 【网络安全】身份验证凭证为何如此重要?
  3. MS CRM 2011 Form与Web Resource在JScript中的相互调用
  4. 模块XX.dll已加载,但对DllRegisterServer的调用失败
  5. 【计算机科学基础】电子计算机抽象层次
  6. USACO-Section1.4 Ski Course Design (枚举)
  7. 【Java】Java趣味分享:try finally
  8. Spring框架----代理的分析
  9. 英伟达美女工程师:AI从业者要不要读博?要不要自己创业?
  10. golang 文件md5_golang 计算大文件md5
  11. C#版 Tag云图控件
  12. 使用Myvatis Generator自动创建项目代码
  13. 使用 LaTeX 语言对 MATLAB 中的图片进行标注
  14. 【数据结构试验】树的基本操作
  15. 奇东锐腾服务器无法显示,奇东锐腾pxe全自动网刻工具
  16. macos masm5 Warning level (0-2) expected after W option
  17. 解读生活中的常用的IC卡
  18. 评论(评价)数据库表的设计
  19. SAP报表导出格式设置
  20. Leetcode 382. Linked List Random Node 以及 水塘抽样算法

热门文章

  1. 展开收起js动画效果
  2. jquery的层次选择器
  3. 将ShaderToy中的Shader搬运到Unity
  4. bzoj 3238: [Ahoi2013]差异(后缀数组+单调栈)
  5. Hive 架构知识体系及语法详解
  6. matlab2c使用c++实现matlab函数系列教程-randint函数
  7. 【python基础语法】列表的查询操作(包括切片)
  8. 尝试AD19从焊盘中间出线
  9. 简单RAM存储器分析
  10. Android现学现用第十二天