这两天在做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. angularJS1笔记-(14)-自定义指令(scope)

    index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...

  2. iOS开发值得收藏的博客

    http://kobedai.me/ objc.io PS:经典,内容深而广objc中国NSHipster PS:很多小细节NSHipster 中文版唐巧的技术博客 PS:LZ是唐巧的脑残粉…OneV ...

  3. paperOne基于java web的简易四则运算出题网站

    项目成员:张金生     张政 需求概要 1.运算数均为正整数 2.包含的运算符有+,-,*,/ 3.除法运算结果为整除运算 4.批量生成题目并判题 核心功能分析 1.题目生成——java后端 题目生 ...

  4. PAT 甲级 1004 Counting Leaves

    https://pintia.cn/problem-sets/994805342720868352/problems/994805521431773184 A family hierarchy is ...

  5. Python中pip install MySQL-python报错解决方法

    环境 Centos 7(其他Centos或者RHEL一样) 问题 在执行 pip install MySQL-python 时报错如: Command "python setup.py eg ...

  6. ZooKeeperACL机制

    官网:http://zookeeper.apache.org/doc/r3.4.6/zookeeperProgrammers.html#sc_ZooKeeperAccessControl 项目中不同的 ...

  7. 用SQL查询方式显示GROUP BY中的TOP解决方法[转]

    用SQL查询方式显示GROUP BY中的TOP怎样用一个SQL语句来显示 分组后每个组的前几位 比如把一个学校所有学生的成绩按班级分组,再显示每个班级前五名的信息. 班级     学生   成绩 一班 ...

  8. 洛谷 P1850 换教室 解题报告

    P1850 换教室 题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有\(2n\)节课程安排在\(n\)个时间段上.在第\(i(1≤i≤n) ...

  9. WEB入门一 网页设计基础

    本章简介 在当今社会,网页是网络信息共享和发布的主要形式.而HTML (Hyper Text Mark-up Language),即超文本标记语言,是创建网页基础.本章将学习HTML文档的基本结构.组 ...

  10. bzoj 2839 : 集合计数 容斥原理

    因为要在n个里面选k个,所以我们先枚举选的是哪$k$个,方案数为$C_{n}^k$ 确定选哪k个之后就需要算出集合交集正为好这$k$个的方案数,考虑用容斥原理. 我们还剩下$n-k$个元素,交集至少为 ...