题目链接

【题解】

把所有的"1"矩形分成m类。
第j类的矩形。他的右边界跟第j列紧靠。
那么。
我们设f[i][j]表示(i,j)这个点往左最长能延伸多少个数目的"1"
那么对于第j类的矩形。
我们会发现。问题转化为求一个侧着放的柱状图。
然后让你在其中找到最大面积的矩形。且要求紧贴着底面(也即第j列)
那么问题就抓换成[这道题](https://www.cnblogs.com/AWCXV/p/11934410.html)了。
做一个O(N)的单调队列就能解决。
所以总的复杂度就是O(N^2)的。

【代码】

class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.empty()) return 0;
if (matrix[0].empty()) return 0;
int n = matrix.size();
int m = matrix[0].size();
int f[1005][1005];
memset(f,0,sizeof(f));
for (int i = 0;i < n;i++)
for (int j = 0;j < m;j++){
if (matrix[i][j]=='1'){
f[i+1][j+1] = f[i+1][j]+1;
}
}
int h[1005],sta[1005];
memset(h,0,sizeof(h));memset(sta,0,sizeof(sta));
int top;
int ma = 0;
for (int j = 1;j <= m;j++){
for (int i = 1;i <= n;i++){
h[i] = f[i][j];
}
top = 1;
sta[1] = 0;
for (int i = 1;i <= n;i++){
while (top!=1 && h[sta[top]]>h[i]){
ma = max(ma,h[sta[top]]*(i-1-sta[top-1]));
top--;
}
sta[++top] = i;
}
while (top>1){
ma = max(ma,h[sta[top]]*(n-sta[top-1]));
top--;
}
}
return ma;
}
};

【LeetCode 85】最大矩形的更多相关文章

  1. Java实现 LeetCode 85 最大矩形

    85. 最大矩形 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 示例: 输入: [ ["1","0","1 ...

  2. leetcode 85. 最大矩形

    题目描述: 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积. 思路分析: 这题是之前那道最大正方形的进阶,同样是利用dp来求解.通过逐行去计算最大矩形,即优化的 ...

  3. LeetCode 85 | 如何从矩阵当中找到数字围成的最大矩形的面积?

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是LeetCode专题53篇文章,我们一起来看看LeetCode中的85题,Maximal Rectangle(最大面积矩形). 今天的 ...

  4. 求解最大矩形面积 — leetcode 85. Maximal Rectangle

    之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...

  5. [LeetCode] 85. Maximal Rectangle 最大矩形

    Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and ...

  6. LeetCode (85): Maximal Rectangle [含84题分析]

    链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...

  7. [LeetCode] Rectangle Area 矩形面积

    Find the total area covered by two rectilinear rectangles in a2D plane. Each rectangle is defined by ...

  8. [LeetCode] Rectangle Overlap 矩形重叠

    A rectangle is represented as a list [x1, y1, x2, y2], where (x1, y1) are the coordinates of its bot ...

  9. Leetcode 492. 构造矩形

    1.题目描述 作为一位web开发者, 懂得怎样去规划一个页面的尺寸是很重要的. 现给定一个具体的矩形页面面积,你的任务是设计一个长度为 L 和宽度为 W 且满足以下要求的矩形的页面.要求: 1. 你设 ...

随机推荐

  1. The server time zone value 'EDT' is unrecognized or represents more than one time zone

    解决: (1)使用 server mysql start命令启动mysql (2)在mysql中执行show variables like '%time_zone%'; (3)输入select now ...

  2. 【ABAP系列】SAP ABAP常用正则表达式大全

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP ABAP常用正则表达式大 ...

  3. JavaScript GetAbsoultURl

    var img = document.createElement('A');     img.src = "/img/weixin.jpg";  // 设置相对路径给Image,  ...

  4. flask为blueprint增加error_handler

    对整个app增加errorhandler,只需如下: @portal_page.errorhandler(404) def page_not_found(error): cats = Category ...

  5. Scala面向对象

    面向对象编程OOP: Scala vs Java 都有这三特性 封装:把属性.方法封装到类中 Person: int id, String name, Date birthday.... 需要gett ...

  6. python学习第二十九天函数局部变量如何改变外部变量

    python函数局部变量如何改变外部变量,之前我说过,局部变量是没办法改变外部变量的,除非局部变量找不到,去外部找,输出变量,使用关键词global 使变量改变外部变量. 1,使用关键词global ...

  7. D Dandan's lunch

    链接:https://ac.nowcoder.com/acm/contest/338/D来源:牛客网 题目描述 As everyone knows, there are now n people pa ...

  8. Iplimage versus Mat

    我们可能经常面临这样的困惑,Iplimage和Mat这两种数据结构,我们应该用哪一种数据结构. Iplimage一开始就存在opencv库之中,他来源于Intel的另外一个函数库Intel Image ...

  9. vue-loader was used without the corresponding plugin. Make sure to include VueLoaderPlugin

    场景: . webpack2.4.*集成vue-loader@15.7.2报错 原因: 参考官方文档 https://vue-loader.vuejs.org/migrating.html#a-plu ...

  10. react native 打包至iphone设备

    1.新建bundle 在自己项目的ios文件夹下新建一个文件夹取名bundle PS:ios文件夹和node_modules文件夹在同一级目录下,这个bundle文件夹名称随意取,后面要用到,但是记得 ...