这两天在做leetcode的题目,最大矩形的题目以前遇到很多次了,一直都是用最笨的方法,扫描每个柱子,变换宽度,计算矩形面积,一直都以为就这样O(n2)的方法了,没有想到居然还有研究出了O(n)的算法,真是对GeeksForGeeks大神膜拜啊。

首先是Largest Rectangle in Histogram这个题目

 class Solution {
public:
int largestRectangleArea(vector<int> &height) {
int len=height.size();
if(len<=) return ;
stack<int> s;
int j=,h,w,maxArea;
while(j<len){
if(s.empty()||height[s.top()]<=height[j]) s.push(j++);//栈空或者有大于栈顶的柱,则一直入栈
else {//计算当前范围内的最大矩形面积
h=height[s.top()];//高度
s.pop();
w=s.empty()?j:j--s.top();//宽度
maxArea=max(maxArea,w*h);
}
}
while(!s.empty()){//遍历栈中的元素,考虑宽度也占优势原因
h=height[s.top()];
s.pop();
w=s.empty()?len:len--s.top();
maxArea=max(maxArea,w*h);
}
return maxArea;
}
};

然后是Maximal Rectangle这个题目,考虑到整个矩阵的情况,我们可以把他当成是二维的Largest Rectangle in Histogram,代码如下:

class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
int row=matrix.size();
if(row==) return ;
int column=matrix[].size();
if(column==) return ; //计算从当前点开始此行中连续的1的个数
int ** dpNum=(int**) malloc(sizeof(int *)*row);
int i,j,k;
for(i=;i<row;i++){
dpNum[i]=(int*)calloc(column,sizeof(int));
for(j=column-;j>=;j--){
if(matrix[i][j]==''){
if(j==column-) dpNum[i][j]=;
else dpNum[i][j]=+dpNum[i][j+];
} else {
dpNum[i][j]=;
}
}
} //按照最大矩形面积计算,使用一个栈保存中间子矩阵遍历高度。
int maxArea=,height=,width=INT_MAX,currentArea=INT_MAX;
for(i=;i<column;i++){ j=;
stack<int> s;
while(j<row){
if(s.empty()||dpNum[j][i]>=dpNum[s.top()][i]) s.push(j++);
else {
width=dpNum[s.top()][i];
s.pop();
height=s.empty()?dpNum[j][i]:dpNum[j][i]-s.top()-;
maxArea=max(maxArea,width*height);
}
} while(!s.empty()){
width=dpNum[s.top()][i];
s.pop();
height=s.empty()?row:row-s.top()-;
maxArea=max(maxArea,width*height);
}
} return maxArea;
}
};

我能说开始的时候我只想到了扫描扫描扫描,很少能考虑到是否有更加高效的方法,真是汗颜啊……

Maximal Rectangle&Largest Rectangle in Histogram的更多相关文章

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

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

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

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

  4. [LeetCode] Largest Rectangle in Histogram O(n) 解法详析, Maximal Rectangle

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

  5. LeetCode 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 85--最大矩形(Maximal Rectangle)

    84题和85五题 基本是一样的,先说84题 84--柱状图中最大的矩形( Largest Rectangle in Histogram) 思路很简单,通过循环,分别判断第 i 个柱子能够延展的长度le ...

  6. [LeetCode] Largest Rectangle in Histogram 直方图中最大的矩形

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

  7. 84. Largest Rectangle in Histogram

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

  8. [LeetCode] 84. Largest Rectangle in Histogram 直方图中最大的矩形

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

  9. poj 2559 Largest Rectangle in a Histogram - 单调栈

    Largest Rectangle in a Histogram Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 19782 ...

随机推荐

  1. 个人作业2——APP案例分析

    产品:网易LOFTER(乐乎)   网易LOFTER是网易旗下图片社交APP,产品覆盖web及移动各端. 网易LOFTER社区内汇聚了多领域的品质生活家与生活达人,包含女神.明星.穿搭.文具.旅行.美 ...

  2. docker搭建redis未授权访问漏洞环境

    这是redis未授权访问漏洞环境,可以使用该环境练习重置/etc/passwd文件从而重置root密码 环境我已经搭好放在了docker hub 可以使用命令docker search ju5ton1 ...

  3. 第二版_TestNG+Excel+(HTTP+JSON) 简单接口测试

    ---------------------------------------------------------------------------------------------------- ...

  4. windows多线程(八) 信号量Semaphore

    如果你看到了这里,我就认为你已经对掌握了有关关键段 CriticalSection.互斥量Mutex和事件Event有关的内容,所以最基本的东西就不再介绍了.如果没有掌握上面说的内容,可以看这里: 关 ...

  5. [转帖] 学习一下 apache bench 的总结简介 ( LAMP的没用过..)

    PS:网站性能压力测试是性能调优过程中必不可少的一环.只有让服务器处在高压情况下才能真正体现出各种设置所暴露的问题.Apache中有个自带的,名为ab的程序,可以对Apache或其它类型的服务器进行网 ...

  6. .NET 切面编程 PostSharp

    目录 概念 实现方式 .Net平台的切面实现 PostSharp示例 概念 Aspect-Oriented Programming(AOP):想想OOP是不是有些熟悉,AOP翻译过来的意思就是面向切面 ...

  7. rpc 协议规范 之 rmi http webservice 和 一些框架

    RPC(Remote Procedure Call)是远程调用,是一种思想,也是一种协议规范.简单地说就是能使应用像调用本地方法一样的调用远程的过程或服务,可以应用在分布式服务.分布式计算.远程服务调 ...

  8. Qt托盘程序

    使用QSystemTrayIcon类可以实现托盘程序.在这里使用QMainWindow做实例: mainwindow.h头文件 #ifndef MAINWINDOW_H #define MAINWIN ...

  9. es各类SearchType的意思

    元素 含义 QUERY_THEN_FETCH 查询是针对所有的块执行的,但返回的是足够的信息,而不是文档内容(Document).结果会被排序和分级,基于此,只有相关的块的文档对象会被返回.由于被取到 ...

  10. 【CF739E】Gosha is hunting(动态规划,凸优化)

    [CF739E]Gosha is hunting(动态规划,凸优化) 题面 洛谷 CF 题解 一个\(O(n^3)\)的\(dp\)很容易写出来. 我们设\(f[i][a][b]\)表示前\(i\)个 ...