这两天在做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. DPDK L3fwd 源码阅读

    代码部分 整个L3fwd有三千多行代码,但总体思想就是在L2fwd的基础上,增加网络层的根据 IP 地址进行路由查找的内容. main.c 文件 int main(int argc, char **a ...

  2. CS学习

    作者:匿名用户链接:https://www.zhihu.com/question/27368268/answer/36464143来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注 ...

  3. Current request is not a multipart request

    1. 文件上传需要在form表单中添加<form enctype="multipart/form-data"> 2. SpringMVC默认是关闭fileupload功 ...

  4. SCRUM:周日周一任务实现情况

    1.设计.制作欢迎界面 2.对杰龙注册界面进行重设计和规范strings     →         →         →    

  5. Alpha阶段敏捷冲刺⑤

    1.提供当天站立式会议照片一张. 每个人的工作 (有work item 的ID),并将其记录在码云项目管理中: 昨天已完成的工作. 新增了一个登录界面 完成记账的分类模块 报表界面初步设计 今天要完成 ...

  6. [转帖]go的调度机制.

    调度器 主要基于三个基本对象上,G,M,P(定义在源码的src/runtime/runtime.h文件中) G代表一个goroutine对象,每次go调用的时候,都会创建一个G对象 M代表一个线程,每 ...

  7. git bash使用(markdown版)

    前言 我是通过这个来学习的.个人愚笨,琢磨了半天,终于搞通了,醉了醉了,以前一直使用svn,用git确实有点水土不服.本文以如何使用git为主来展开,不涉及太多理论. git是分布式的版本管理.什么叫 ...

  8. File FileStream StreamReader StreamWriter C#

    存在各种各样的IO设备,比如说文件File类(字符串文件和二进制文件),可以直接使用File类对文件进行读写操作. 这些各种IO的读取和写入是通过流的形式实现的,基类为Stream,针对各种不同的IO ...

  9. token是干啥子的

    http://www.cnblogs.com/wweichao/p/9325668.html 在上面这篇博客中,我们知道了通过weibo提供的一系列接口,我们可以实现登录,然后也有了token,可以获 ...

  10. 相见恨晚的 scala - 01 [ 基础 ]

    简洁到不行,多一个分号都是不应该. 学习笔记: centOS 下安装 scala 和安装 jdk 一毛一样 . 1 . 不同于 Java 的变量声明 :( 但是和 js 很像 ) /** * Crea ...