题目描述:

给定n个非负整数height[n],分别代表直方图条的高,每个条的宽设为1,求直方图中面积最大的矩形的面积

题目来源:

http://oj.leetcode.com/problems/largest-rectangle-in-histogram/

题目分析:

维护一个栈,保存直方图条的下标,当当前栈为空或者栈顶的下标所表示的元素不大于当前元素时,入栈,否则出栈,直到可以把当前元素压入栈中

(1)对于当前栈,假设序列为a1, a2,...ai, ai+1, a...栈顶,那么处于ai和ai+1之间的元素一定大于ai+1,如果他们中的最小元素小于等于ai+1,那么它一定在栈中,故栈中处于ai和ai+1之间的元素一定大于ai+1

(2)计算矩形的面积,可以考虑以待计算的元素为中心,向右扩展最远,并且向左扩展最远

(3)出栈时,计算以刚出栈的元素为高的最大矩形,它向左扩展最远到栈中的下一个元素,向右扩展最远到当前元素(因为当前元素比他小)

时间复杂度:O(n)
示例代码:
int maxArea(vector<int> vi) {
stack<int> st;
int maxArea = , i = ; while(i <= n) {
if(st.empty() || vi[st.top()] <= vi[i]) {
st.push(i++);
} else {
int tmp = st.top();
st.pop();
maxArea = max(maxArea, vi[tmp] * (st.empty() ? i : i - st.top() - ));
}
} return maxArea;
}

Largest Rectangle in Histogram-最大长方形的更多相关文章

  1. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  2. leetcode之Largest Rectangle in Histogram

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

  3. LeetCode 笔记系列 17 Largest Rectangle in Histogram

    题目: Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar he ...

  4. 47. Largest Rectangle in Histogram && Maximal Rectangle

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

  5. 【LeetCode】84. Largest Rectangle in Histogram

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

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

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

  7. LeetCode之“动态规划”:Maximal Square && Largest Rectangle in Histogram && Maximal Rectangle

    1. Maximal Square 题目链接 题目要求: Given a 2D binary matrix filled with 0's and 1's, find the largest squa ...

  8. 84. Largest Rectangle in Histogram

    https://www.cnblogs.com/grandyang/p/4322653.html 1.存储一个单调递增的栈 2.如果你不加一个0进去,[1]这种情况就会输出结果0,而不是1 3.单调递 ...

  9. LeetCode: Largest Rectangle in Histogram 解题报告

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

  10. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

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

随机推荐

  1. Mac系统如何配置adb

    在使用mac进行android开发之前,我们一般会安装android studio 或者 eclipse,无论哪一款开发软件,都少不了安装adb(Android Debug Bridge).adb(A ...

  2. 三门概率问题之C#版

    前言: 早上看到一片关于三门问题的博客http://www.cnblogs.com/twocats/p/3440398.html,抱着该博客结论的怀疑态度用C#语言写了一些代码.实验证明该博客的结论是 ...

  3. hibernate--联合主键(了解+,掌握-)

    如果一个表有多个主键(= =一般比较少) 8.4. 组件作为联合标识符(Components as composite identifiers) 先定义一个类OrderLineId (实现接口,imp ...

  4. 初用Spring Test

    粗糙的研究了下Spring test,分享以下blog: 1. http://blog.csdn.net/shan9liang/article/details/40452469 2. http://w ...

  5. 剑指offer--面试题20

    题目:从外向里顺时针打印矩阵 做题心得:该题本质上并未考查复杂的数据结构及算法,而是考查了快速找规律的能力!!! 要想作出此题,必须先有绝对清晰的思路,否则越写越乱(因为涉及到很多的循环打印) 自己当 ...

  6. Leetcode#109 Convert Sorted List to Binary Search Tree

    原题地址 跟Convert Sorted Array to Binary Search Tree(参见这篇文章)类似,只不过用list就不能随机访问了. 代码: TreeNode *buildBST( ...

  7. iOS开发如何实现消息推送机制

    一.关于推送通知 推送通知,也被叫做远程通知,是在iOS 3.0以后被引入的功能.是当程序没有启动或不在前台运行时,告诉用户有新消息的一种途径,是从外部服务器发送到应用程序上的.一般说来,当要显示消息 ...

  8. C++实现CString和string的互相转换

    CString->std::string 例子: CString strMfc=“test“; std::string strStl; strStl=strMfc.GetBuffer(0); u ...

  9. protobuf的安装和使用

    以下全部基于win7系统. protobuf是什么,有什么用网上说的已经很多了.这里就是说一下怎么使用.就当给自己做个笔记吧. .proto文件的语法什么的也请网上查看,挺多的. 第一步: 下载pro ...

  10. IIS Express 及 vs2008下使用IIS Express

    介绍 IIS Express 开发 ASP.NET 的应用程序是我的主要工作.当然我会选择最适合的开发环境.客户多属于企业用户,我的开发的选择,多半是 ASP.NET Web Application ...