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 height = [2,1,5,6,2,3],
return 10.

方法一:两层循环遍历,复杂度O(n2

 class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int maxArea=;
for(unsigned i=; i<height.size(); i++)
{
int min = height[i];
for(unsigned j=i; j<height.size(); j++)
{
if(height[j]<min)
min = height[j];
int area = min*(j-i+);
if(area>maxArea)
maxArea = area;
}
}
return maxArea;
}
};

方法二:用堆栈保存重要位置,复杂度O(n)

 class Solution {
public:
int largestRectangleArea(vector<int> &height) { //用堆栈来实现
stack<unsigned> st;
unsigned maxArea = ;
for(unsigned i=; i<height.size(); i++)
{
if(st.empty())
st.push(i);
else
{
while(!st.empty())
{
if(height[i]>=height[st.top()])
{
st.push(i);
break;
}
else
{
unsigned idx=st.top();
st.pop();
unsigned leftwidth = st.empty() ? idx : (idx-st.top()-);
unsigned rightwidth = i - idx-;
maxArea = max(maxArea, height[idx]*(leftwidth+rightwidth+));
}
}
if(st.empty())
st.push(i);
}
}
unsigned rightidx = height.size();
while(!st.empty())
{
unsigned idx = st.top();
st.pop();
unsigned leftwidth = st.empty() ? idx : (idx-st.top()-);
unsigned rightwidth = rightidx - idx-;
maxArea = max(maxArea, height[idx]*(leftwidth+rightwidth+));
}
return maxArea;
}
};

[LeetCode OJ] Largest Rectangle in Histogram的更多相关文章

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

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

  2. leetcode之Largest Rectangle in Histogram

    问题来源:Largest Rectangle in Histogram 问题描述:给定一个长度为n的直方图,我们可以在直方图高低不同的长方形之间画一个更大的长方形,求该长方形的最大面积.例如,给定下述 ...

  3. 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 ...

  4. 关于LeetCode的Largest Rectangle in Histogram的低级解法

    在某篇博客见到的Largest Rectangle in Histogram的题目,感觉蛮好玩的,于是想呀想呀,怎么求解呢? 还是先把题目贴上来吧 题目写的很直观,就是找直方图的最大矩形面积,不知道是 ...

  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 (最大矩形直方图) 解题思路和方法

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

  7. LeetCode之Largest Rectangle in Histogram浅析

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

  8. [LeetCode#84]Largest Rectangle in Histogram

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

  9. LeetCode 84. Largest Rectangle in Histogram 直方图里的最大长方形

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

随机推荐

  1. use isSubstring to check if one word is a rotation of another.

      1: /// <summary> 2: /// Assume you have a method isSubstring which checks if one word is a s ...

  2. js发起长轮询获取推送消息

    <!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...

  3. linux杂谈(十九):DNSserver的配置(二)

    1.本机和外网的解析 ​ ​之前讨论了DNS的正向解析和反向解析,但有的时候我们想让DNSserver对于server本机的解析和对于外网的解析是不同的.那就须要在主配置文件里对本机和外网进行不同的配 ...

  4. MVC-控制器向View传值的三种方法

    1.提供视图模型对象 你能把一个对象作为View方法的参数传递给视图. public ViewResult Index() { DateTime date = DateTime.Now; return ...

  5. FastDFS 的部署、配置与测试的

    部署篇:http://soartju.iteye.com/blog/803477 配置篇:http://soartju.iteye.com/blog/803524 测试篇:http://soartju ...

  6. [WebGL入门]十四,绘制多边形

    注意:文章翻译http://wgld.org/.原作者杉本雅広(doxas),文章中假设有我的额外说明,我会加上[lufy:].另外,鄙人webgl研究还不够深入.一些专业词语,假设翻译有误,欢迎大家 ...

  7. C++之枚举

    1. 声明枚举类型格式 enum Day{ Mon,Tue=5,Wed};//Mon=0;Tue=5;Wed=6 enumDay1{Mon1,Tue1,Wed1};//Mon1=0;Tue1=1;We ...

  8. Linux如何卸载挂载文件

    在我们进行远程文件操作的时候,我们经常会出现文件服务出现卸载掉哦情况.例如 umount /mnt/net1umount: /mnt/net1: device is busy.        (In ...

  9. MegaCli监控RAID磁盘健康信息

    首先有必要先介绍一下有关RAID卡的使用,有关Cache问题也有个人理解:生产系统上的物理机磁盘配置大多都是RAID5,之前会经常性的遇到磁盘IO在某个时间极度的跟不上节奏,稍微写入量,磁盘utils ...

  10. 调试php的soapCient

    try { import('@.Ext.xml'); header("Content-Type:text/html; charset=utf-8"); $soap = new So ...