题目描述:

给定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. Binary Tree Zigzag Level Order Traversal

    Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ...

  2. 向Array中添加冒泡排序

    冒泡排序思想 通过在无序区的相邻元素的比较和替换,使较小的元素浮到最上面. 冒泡排序实现 Function.prototype.method = function(name, func){ this. ...

  3. Careercup - Google面试题 - 5377673471721472

    2014-05-08 22:42 题目链接 原题: How would you split a search query across multiple machines? 题目:如何把一个搜索que ...

  4. 图解SQL的Join(转摘)

    转摘网址:http://coolshell.cn/articles/3463.html 对于SQL的Join,在学习起来可能是比较乱的.我们知道,SQL的Join语法有很多inner的,有outer的 ...

  5. 第六周博客技术发表 C语言代码

    #include <stdio.h>       /*使用printf要包含的头文件*/#include <conio.h>void main(void)        /*主 ...

  6. 【CodeForces】【311C】Fetch the Treasures

    最短路 神题一道…… //CF 311C #include<queue> #include<cstdio> #include<cstdlib> #include&l ...

  7. Leetcode#91 Decode Ways

    原题地址 动态规划题,注意0导致的小陷阱. 代码: int numDecodings(string s) { ] < ] > ; ] >= ] <= : ; ; int nex ...

  8. java socket 一个服务器对应多个客户端,可以互相发送消息

    直接上代码,这是网上找的demo,然后自己根据需求做了一定的修改.代码可以直接运行 服务器端: package socket; import java.io.BufferedReader; impor ...

  9. [工作积累] OpenGL ES3.0: glInvalidateFramebuffer

    https://www.khronos.org/opengles/sdk/docs/man3/html/glInvalidateFramebuffer.xhtml 这个在GLES2.0上只有Exten ...

  10. 最小PE文件讨论

    1.实例1国外的人写的最小的PE文件--97Bytes 4D5A0000504500004C0101006A2A58C30000000000000000040003010B01080004000000 ...