题目:

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的更多相关文章

  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. 一篇文章彻底弄清ARC始末

    本文转载至 http://blog.csdn.net/allison162004/article/details/38758265 自动引用计数(ARC)是编译器的一个特色,提供了Objective- ...

  2. [Matlab绘图][三维图形][三维曲线基本函数+三维曲面+其他三维图形]

    1.绘制三维图形的基本函数 最基本的三维绘图函数为plot3: plot3与plot用法十分相似,调用格式: plot(x1,y1,z1,选项1,x2,y2,z2,选项2,...,xn,yn,zn,选 ...

  3. css 坑记

    1. div 内容超出 (做换行处理)   要注意 white-space属性的运用 设置 div width:100%;(或者固定值) 设置换行  word-break: break-all; 设置 ...

  4. 九度OJ 1345:XXX定律之画X (递归)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:361 解决:157 题目描述: 给你一个n,然后让你输出F(n) 规则是这样的,F(n)的输出结果是: F(n-1)     F(n-1) ...

  5. 洛谷 P1558 色板游戏

    洛谷 题解里面好像都是压位什么的, 身为蒟蒻的我真的不会, 所以就来谈谈我的30颗线段树蠢方法吧! 这题初看没有头绪. 然后发现颜色范围好像只有30: 所以,我就想到一种\(sao\)操作,搞30颗线 ...

  6. 次小生成树Tree

    次小生成树Treehttps://www.luogu.org/problemnew/show/P4180 题目描述 小C最近学了很多最小生成树的算法,Prim算法.Kurskal算法.消圈算法等等.正 ...

  7. Django之CURD插件2

    目标:达到下图拥有功能的实现 1.绑定编辑按钮 ************思路**************** 1.为编辑按钮添加样式,可以根据样式来进行判断在什么状态. 2.进入编辑模式,将可编辑的字 ...

  8. Linux软件包分类

    源代码包 优点: 1.给你的就是源代码 2.可以修改源代码 3.可以自由选择所需的功能 4.软件是在自己电脑上编译安装,所以更加稳定高效 5.卸载方便(直接删了你安装软件的那个目录就好了) 缺点: 1 ...

  9. 七招从办公室政治中取胜 发表于 09 May 2008 ? 领导力培养

    办公室政治,对有些人来说是一个禁忌词汇,但在工作场合它却不可回避.简单说来,它就是职场上人与人的不同:观念的差异.利益的冲突 都可以看成是办公室政治的表现.它等于人与人之间的交流和关系.没必要害怕办公 ...

  10. 尽量不要在viewWillDisappear:方法中移除通知

    1.iOS7新增加了导航控制器侧滑手势,当触发侧滑返回时,会调用系统的viewWillDisappear:方法,取消侧滑返回时又会调用viewWillAppear:方法.   2.在做手势和通知等一系 ...