题目:

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.

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

题解:

Solution  1

  1. class Solution {
  2. public:
  3. int largestRectangleArea(vector<int>& heights) {
  4. if(heights.size() < )
  5. return ;
  6. return maxArea(heights, , heights.size() - );
  7. }
  8. private:
  9. int combineArea(vector<int> &heights, int l, int m, int r){
  10. int i = m, j = m;
  11. int area = , h = min(heights[i], heights[j]);
  12. while(i >= l && j <= r){
  13. h = min(h, min(heights[i], heights[j]));
  14. area = max(area, h * (j - i + ));
  15. if(i == l)
  16. ++j;
  17. else if(j == r)
  18. --i;
  19. else {
  20. if(heights[i - ] > heights[j + ])
  21. --i;
  22. else
  23. ++j;
  24. }
  25. }
  26.  
  27. return area;
  28. }
  29.  
  30. int maxArea(vector<int> &heights, int l, int r){
  31. if(l >= r)
  32. return heights[l];
  33. int m = l + (r - l) / ;
  34. int area = maxArea(heights, l, m - );
  35. area = max(area, maxArea(heights, m + , r));
  36. area = max(area, combineArea(heights, l, m, r));
  37. return area;
  38. }
  39. };

Solution  2 摘自geeks ,用到了单调栈的思想

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

Solution  3 优化版

  1. class Solution {
  2. public:
  3. int largestRectangleArea(vector<int>& heights) {
  4. int res = ;
  5. stack<int> s;
  6. int n = heights.size();
  7. heights.push_back();
  8. for(int i = ; i <= n;){
  9. if(s.empty() || heights[s.top()] < heights[i]){
  10. s.push(i++);
  11. } else {
  12. int cur = s.top();s.pop();
  13. int area_top = heights[cur] * (s.empty() ? i : i - s.top() - );
  14. res = max(res, area_top);
  15. }
  16. }
  17. return res;
  18. }
  19. };

Solutin 4 实际上有些类似,不过把栈的空间复杂度转化为求面积时的时间复杂度,实际上花费时间要多,不推荐此做法。

  1. class Solution {
  2. public:
  3. int largestRectangleArea(vector<int> &heights) {
  4. int res = , n = heights.size() - ;
  5. for (int i = ; i < heights.size(); ++i) {
  6. if (i < n - && heights[i] <= heights[i + ]) {
  7. continue;
  8. }
  9. int h = heights[i];
  10. for (int j = i; j >= ; --j) {
  11. h = min(h, heights[j]);
  12. int area = h * (i - j + );
  13. res = max(res, area);
  14. }
  15. }
  16. return res;
  17. }
  18. };

Solution 4 摘自geeks  基于线段树

【LeetCode】084. Largest Rectangle in Histogram的更多相关文章

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

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

  2. 【LeetCode】84. Largest Rectangle in Histogram

    Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...

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

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

  4. 【Leetcode】84. Largest Rectangle in Histogram 85. Maximal Rectangle

    问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...

  5. 【一天一道LeetCode】#84. Largest Rectangle in Histogram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...

  6. 【Lintcode】122.Largest Rectangle in Histogram

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

  7. 【LeetCode】764. Largest Plus Sign 解题报告(Python)

    [LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...

  8. 【LeetCode】813. Largest Sum of Averages 解题报告(Python)

    [LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  9. Java for LeetCode 084 Largest Rectangle in Histogram【HARD】

    For example, Given height = [2,1,5,6,2,3], return 10. 解题思路: 参考Problem H: Largest Rectangle in a Hist ...

随机推荐

  1. Spring MVC 返回Json IE出现下载

    今天在做一个利用IFrame提交进行form提交表单的时候发现返回的json在ie下竟然弹出了下载的提示, 于是就查看了返回的Content-type:appliation/json;charset= ...

  2. task15-18

    [说明]貌似maven在真实的项目实战中挺重要的,可以省去大量的工作,有必要单独学习一下 15.创建一个新的maven项目 16.在src/main/java下随便创建一个java文件,clean,i ...

  3. Web框架的引入

    为什么会有web框架 有了上一篇内容,静态.动态web服务器的实现,已经掌握了客户端请求到服务器处理的机制.在动态资源处理中,根据请求 .py 导入模块应用,然后调用应用入口程序实现动态处理.但是在真 ...

  4. nginx学习之静态内容篇(五)

    1.根目录和索引文件 server { root /www/data; location / { } location /images/ { } location ~ \.(mp3|mp4) { ro ...

  5. Lock和Condition

    1 什么是可重入锁 可重入锁是说一个线程在已经获取了该锁的情况下,还可以再次获取该锁. 主要的应用场景: 可重入锁指的是在一个线程中可以多次获取同一把锁,比如:一个线程在执行一个带锁的方法,该方法中又 ...

  6. java 死锁产生原因及解锁(转)

    原文地址 进程死锁及解决办法 一.要点提示 (1) 掌握死锁的概念和产生死锁的根本原因. (2) 理解产生死锁的必要条件--以下四个条件同时具备:互斥条件.不可抢占条件.占有且申请条件.循环等待条件. ...

  7. matlab + c/c++ opencv 混合编程

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 辛苦原创所得,转载请注明出处 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ...

  8. runtime-分类为什么不生成setter和getter

    前言 前几天有人问我一个问题:为什么分类不能自动创建get set方法.老实说,笔者从来没有去思考过这个问题.于是这次通过代码实践跟runtime源码来探究这个问题. 准备工作 为了能减少输出类数据的 ...

  9. CSS3图片悬停放大动画

    在线演示 本地下载

  10. Vim 的命令模式转插入模式

    一.在命令模式输入下面的快捷方式: i 在当前光标前插入字符: I 在当前行行首插入字符: a 在当前光标后插入字符: A 在当前行行尾插入字符: o 在当前行下面另起一新行: O 在当前行上面另起一 ...