【LeetCode】084. Largest Rectangle in Histogram
题目:
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
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        if(heights.size() < )
            return ;
        return maxArea(heights, , heights.size() - );
    }
private:
    int combineArea(vector<int> &heights, int l, int m, int r){
        int i = m, j = m;
        int area = , h = min(heights[i], heights[j]);
        while(i >= l && j <= r){
            h = min(h, min(heights[i], heights[j]));
            area = max(area, h * (j - i + ));
            if(i == l)
                ++j;
            else if(j == r)
                --i;
            else {
                if(heights[i - ] > heights[j + ])
                    --i;
                else
                    ++j;
            }
        }
        return area;
    }
    int maxArea(vector<int> &heights, int l, int r){
        if(l >= r)
            return heights[l];
        int m = l + (r - l) / ;
        int area = maxArea(heights, l, m - );
        area = max(area, maxArea(heights, m + , r));
        area = max(area, combineArea(heights, l, m, r));
        return area;
    }
};
Solution 2 摘自geeks ,用到了单调栈的思想
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int res = , area_top = ;
        stack<int> s;
        int n = heights.size();
        int i = ;
        for(i = ; i < n;){
            if(s.empty() || heights[s.top()] < heights[i]){
                s.push(i++);
            } else {
                int cur = s.top();s.pop();
                area_top = heights[cur] * (s.empty() ? i : i - s.top() - );
                res = max(res, area_top);
            }
        }
        while(!s.empty()){
            int cur = s.top();s.pop();
            area_top = heights[cur] * (s.empty() ? i : i - s.top() - );
            res = max(res, area_top);
        }
        return res;
    }
};
Solution 3 优化版
class Solution {
public:
    int largestRectangleArea(vector<int>& heights) {
        int res = ;
        stack<int> s;
        int n = heights.size();
        heights.push_back();
        for(int i = ; i <= n;){
            if(s.empty() || heights[s.top()] < heights[i]){
                s.push(i++);
            } else {
                int cur = s.top();s.pop();
                int area_top = heights[cur] * (s.empty() ? i : i - s.top() - );
                res = max(res, area_top);
            }
        }
        return res;
    }
};
Solutin 4 实际上有些类似,不过把栈的空间复杂度转化为求面积时的时间复杂度,实际上花费时间要多,不推荐此做法。
class Solution {
public:
    int largestRectangleArea(vector<int> &heights) {
        int res = , n = heights.size() - ;
        for (int i = ; i < heights.size(); ++i) {
            if (i < n -  && heights[i] <= heights[i + ]) {
                continue;
            }
            int h = heights[i];
            for (int j = i; j >= ; --j) {
                h = min(h, heights[j]);
                int area = h * (i - j + );
                res = max(res, area);
            }
        }
        return res;
    }
};
Solution 4 摘自geeks 基于线段树
【LeetCode】084. Largest Rectangle in Histogram的更多相关文章
- 【LeetCode】84. Largest Rectangle in Histogram 柱状图中最大的矩形(Python)
		
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 单调栈 日期 题目地址: https://leetc ...
 - 【LeetCode】84. Largest Rectangle in Histogram
		
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
 - 【LeetCode】84. Largest Rectangle in Histogram——直方图最大面积
		
Given n non-negative integers representing the histogram's bar height where the width of each bar is ...
 - 【Leetcode】84. Largest Rectangle in Histogram    85. Maximal Rectangle
		
问题描述: 84:直方图最大面积. 85:0,1矩阵最大全1子矩阵面积. 问题分析: 对于84,如果高度递增的话,那么OK没有问题,不断添加到栈里,最后一起算面积(当然,面积等于高度h * disPo ...
 - 【一天一道LeetCode】#84. Largest Rectangle in Histogram
		
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given n ...
 - 【Lintcode】122.Largest Rectangle in Histogram
		
题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...
 - 【LeetCode】764. Largest Plus Sign 解题报告(Python)
		
[LeetCode]764. Largest Plus Sign 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn ...
 - 【LeetCode】813. Largest Sum of Averages 解题报告(Python)
		
[LeetCode]813. Largest Sum of Averages 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
 - 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 ...
 
随机推荐
- sql server 2008 去除html标签
			
由于商品详情数据库的字段是text,存放的是html,但是要求导出的商品详情中只是商品的描述,不要标签,原来打算先把数据导入excel中,然后利用java的正则去替换,结果由于商品详情太大,一个单元格 ...
 - P1009 阶乘之和
			
P1009 阶乘之和 题目提供者洛谷OnlineJudge 标签数论(数学相关)高精1998NOIp提高组NOIp普及组 难度普及- 通过/提交1139/3791 提交该题 讨论 题解 记录 题目描述 ...
 - js跨浏览器复制: ZeroClipboard
			
实例结构: demo.html <script type="text/javascript" src='http://code.jquery.com/jquery.js'&g ...
 - js与jquey的表达
			
jquery 1.$("#id").css("display") 2.$(this) 3.attr(a,b) :在a里面追加元素b 4.prop: functi ...
 - 说说JSON和JSONP,也许你会豁然开朗,含jQuery用例(转载)
			
前言: 说到AJAX就会不可避免的面临两个问题,第一个是AJAX以何种格式来交换数据?第二个是跨域的需求如何解决?这两个问题目前都有不同的解决方案,比如数据可以用自定义字符串或者用XML来描述,跨域 ...
 - Oracle视图传递参数
			
在Oracle里,视图不像存储过程和函数一样,可以定义输入参数,但我们可以变个方式,使用程序包来实现. oracle package: oracle package是oracle包,是一组相关过程.函 ...
 - vim下的ctags和taglist等的使用和配置
			
1.ctags (1)到 http://prdownloads.sourceforge.net/ctags/ctags-5.6.tar.gz 下载ctags源码ctags-5.6.ta ...
 - vim中使用sed去除网上copy的源代码行号和空格
			
有些时候,在网上搜索到的代码都包含有行号,高亮显示控件不支持直接提取,如: test.sh 01 #!/bin/bash 02 echo “aaa” 简单的去掉行号和前面的空格: 方案一: 1.vim ...
 - 【转载】Java定时器的学习
			
前几看了一下<thinking in java>了解到java原生的Times类有两个问题: (1)Timer是启动单个线程来处理所有的时间任务,如果一个任务耗时很久,那么如果在执行这个过 ...
 - struts2核心和工作原理
			
转至:http://blog.csdn.net/laner0515/article/details/27692673 在学习struts2之前,首先我们要明白使用struts2的目的是什么?它能给我们 ...