这两天在做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. Alpha版本冲刺(七)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  2. 初入React(一)

    React:是2013年Facebook在github上的一个开源js库,它将用户界面抽象为一个个组件,再由开发者将其组合成页面.它不是完整的MVC/MVVM框架,专注于提供清晰.简洁的view层解决 ...

  3. js对数组进行浅复制,深复制的方法

    js 数组常用方法,数组的拷贝(不影响原数组),数组相等 置顶2017年07月17日 17:39:26 阅读数:4640 改变原数组的方法: pop();删除尾部的第一个元素并且返回这个元素: var ...

  4. pygame学习笔记(4)——声音

    转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi pygame.mixer是一个用来处理声音的模块,其含义为“混音器”.游戏中对声音的处理一般包括制造声音和播放声音 ...

  5. vsftpd重启失败解决方法

    vsftpd dead,but subsys locked vsftpd已死,但是subsys被锁 关于VSFTPD服务器重启失败,研究了一晚上,当virtual_use_local_privs=ye ...

  6. Kafka高可用实现

    数据存储格式 Kafka的高可靠性的保障来源于其健壮的副本(replication)策略.一个Topic可以分成多个Partition,而一个Partition物理上由多个Segment组成. Seg ...

  7. 数据结构开发(14):KMP 子串查找算法

    0.目录 1.KMP 子串查找算法 2.KMP 算法的应用 3.小结 1.KMP 子串查找算法 问题: 如何在目标字符串S中,查找是否存在子串P? 朴素解法: 朴素解法的一个优化线索: 示例: 伟大的 ...

  8. Callable 和 Runnable 的区别

    Callable 和 Runnable 的使用方法大同小异, 区别在于: 1.Callable 使用 call() 方法, Runnable 使用 run() 方法 2.call() 可以返回值, 而 ...

  9. python selenium2 有关cookie操作实例及如何绕开验证码

    1.先看一下cookie是啥 cookie是访问web时服务器记录在用户本地的一系列用户信息(比如用户登录信息),以便对用户进行识别 from selenium import webdriver im ...

  10. Hive权限管理

    最近遇到一个hive权限的问题,先简单记录一下,目前自己的理解不一定对,后续根据自己的理解程度更新 一.hive用户的概念 hive本身没有创建用户的命令,hive的用户就是Linux用户,若当前是用 ...