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. POJ 3186Treats for the Cows (区间DP)

    详见代码 #include <stdio.h> #include <algorithm> #include <string.h> using namespace s ...

  2. HTML5 Canvas核心技术—图形、动画与游戏开发.pdf7

    性能 运行putImageData()比drawImage()慢,同等条件下优先考虑drawImage() 操作图像数据需要遍历大量数据,应该注意几点: 1)避免在循环体中直接访问对象属性,应当保存在 ...

  3. 南阳acm奇偶数分离

    这道题的特殊要求是要先先限定了测试数据的组数,所以多加一条循环语句.下面是已通过的代码: #include<stdio.h>   main() {         int n,m,i,j; ...

  4. javascript获取选中的文本/html

    首先来谈一下Selection对象和Range对象. Selection是window.getSelection()方法返回的一个对象,用于表示用户选中的文本区域.Selection对象表现为一组Ra ...

  5. S2SH邮件注册激活后注册成功

    首先我的思路是这样的:①接收从客户端接收过来的数据(密码,用户名,邮箱) ②将密码进行MD5加密,然后将信息用"_"连接起来(用于后面分解) ③将信息交个一个工具类中实现生成邮件信 ...

  6. Java开发常用代码

    1. 字符串有整型的相互转换 String a = String.valueOf(2); //integer to numeric string int i = Integer.parseInt(a) ...

  7. 在Excel中创建和使用ServerDocument

    ServerDocument是微软提供的一种读取Word或Excel文档级应用中CachedData的工具.本示例将向你展示如何使用用ServerDocument. 1.      创建文档级应用 打 ...

  8. jvisualvm图解

    http://blog.csdn.net/a19881029/article/details/8432368 jvisualvm能干什么:监控内存泄露,跟踪垃圾回收,执行时内存.cpu分析,线程分析. ...

  9. springMVC工作原理图

  10. 仿QQ迷你首页(VC++,MFC)(迷你资讯)的开发与实现(源代码)

    由于需求,需要写个类似QQ迷你资讯首页的东西,就花了一点时间写了写,软件效果截图如下: 程序的主要核心代码如下: 程序的全部源代码下载地址:http://download.csdn.net/downl ...