一、题目说明

题目,85. Maximal Rectangle,计算只包含1的最大矩阵的面积。难度是Hard!

二、我的解答

看到这个题目,我首先想到的是dp,用dp[i][j]表示第i行第j列元素向右下角计算的最大面积。后来发现从dp[i+1][j]dp[i][j+1]dp[i+1][j+1]计算dp[i][j]几乎没有任何规律可循。

然后,我就想用down_dp[i][j]right_dp[i][j]两个dp,但遗憾的是还是没成功。

后面看了大神的写法,其实down_dp[i][j]然后“向右找同一行”计算即可。代码如下:

class Solution{
public:
int maximalRectangle(vector<vector<char>>& matrix){
if(matrix.empty()) return 0;
int m = matrix.size();
int n = matrix[0].size(); vector<vector<int>> down_dp(m,vector<int>(n,0));
int result = 0;
//最后一行
for(int j=n-1;j>=0;j--){
if(matrix[m-1][j]=='0'){
down_dp[m-1][j] = 0;
}else if(j==n-1){
down_dp[m-1][j] = 1;
result = max(1,result);
}else{
down_dp[m-1][j] = 1;
result = max(1,result);
int tmp = 1;
for(int t=j+1;t<n;t++){
if(down_dp[m-1][t]>0){
tmp++;
result = max(tmp,result);
}else{
break;
}
}
}
} //最后一列
for(int i=m-1;i>=0;i--){
if(matrix[i][n-1]=='0'){
down_dp[i][n-1] = 0;
}else if(i==m-1){
down_dp[i][n-1] = 1;
result = max(1,result);
}else{
down_dp[i][n-1] = down_dp[i+1][n-1] + 1;
result = max(down_dp[i][n-1],result);
}
} for(int j=n-1;j>=0;j--){//列
for(int i=m-2;i>=0;i--){
if(matrix[i][j]=='0'){
down_dp[i][j] = 0;
}else if(matrix[i][j]=='1'){
down_dp[i][j] = down_dp[i+1][j] + 1;
result = max(down_dp[i][j],result);
int temp = 1,curMin=down_dp[i][j],curMax = down_dp[i][j]; //向右找同一行
for(int t=j+1;t<n;t++){
if(down_dp[i][t]>0){
temp++;
curMin = min(curMin,down_dp[i][t]);
curMax = temp * curMin;
result = max(curMax,result);
}else{
break;
}
}
}
}
} return result;
}
};

性能如下:

Runtime: 28 ms, faster than 45.92% of C++ online submissions for Maximal Rectangle.
Memory Usage: 11.1 MB, less than 61.11% of C++ online submissions for Maximal Rectangle.

三、优化措施

上面代码,先计算最后一行,最后一列,然后向上计算。其实完全可以合并起来的。

class Solution{
public:
int maximalRectangle(vector<vector<char>>& matrix){
if(matrix.empty()) return 0;
int m = matrix.size();
int n = matrix[0].size(); vector<vector<int>> down_dp(m,vector<int>(n,0));
int result = 0; for(int j=n-1;j>=0;j--){//列
for(int i=m-1;i>=0;i--){
if(matrix[i][j]=='0'){
down_dp[i][j] = 0;
}else if(matrix[i][j]=='1'){
if(i<m-1){
down_dp[i][j] = down_dp[i+1][j] + 1;
} else{
down_dp[i][j] = 1;
} result = max(down_dp[i][j],result);
int temp = 1,curMin=down_dp[i][j],curMax = down_dp[i][j]; //向右找同一行
for(int t=j+1;t<n;t++){
if(down_dp[i][t]>0){
temp++;
curMin = min(curMin,down_dp[i][t]);
curMax = temp * curMin;
result = max(curMax,result);
}else{
break;
}
}
}
}
} return result;
}
};

刷题85. Maximal Rectangle的更多相关文章

  1. 85. Maximal Rectangle

    85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...

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

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

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

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

  4. 【leetcode】85. Maximal Rectangle(单调栈)

    Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...

  5. 85. Maximal Rectangle (Graph; Stack, DP)

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

  6. 【LeetCode】85. Maximal Rectangle

    Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...

  7. 85. Maximal Rectangle 由1拼出的最大矩形

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

  8. [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 ...

  9. 【LeetCode】85. Maximal Rectangle 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...

随机推荐

  1. MySQL日志及索引

    MySQL物理结构: MySQL它是通过文件系统对数据进行储存和管理,从物理结构上分为日志文件和数据文件 日志文件: 日志文件记录了数据库操作的信息和一些错误信息,我们常用的日志文件有:错误日志.二进 ...

  2. .net core之编辑json配置文件

    .net core之编辑json配置文件 引言 最近在具体项目开发应用中,项目采用的json格式配置文件,配置文件的加载采用的IConfiguration接口对象进行的管理,这是.net standa ...

  3. latex2e

    classs and packages 这一节介绍新的latex文档结构, 以及新的文件类型: classs and packages 类和包是什么? lext 2.09和latex2e的主要差别就在 ...

  4. 【WPF学习】第二十一章 特殊容器

    内容控件不仅包括基本控件,如标签.按钮以及工具提示:它们还包含特殊容器,这些容器可用于构造用户界面中比较大的部分区域. 首先介绍ScrollViewer控件,该控件直接继承自ContentContro ...

  5. 使用Python写的WingPro7 Pyside2 和 PyQt5插件

    pyside2的 import wingapi import subprocess pyside2_uic = "pyside2-uic" pyside2_qrc = " ...

  6. CSS-08-边框属性设置

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  7. Spring Boot动态注入删除bean

    Spring Boot动态注入删除bean 概述 因为如果采用配置文件或者注解,我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就得采用动态处理bean,包括:动态注入,动态删除. 动态注入 ...

  8. IO博客专栏

    1. IO概览 2. 字符流与字节流的区别

  9. JVM源码分析之警惕存在内存泄漏风险的FinalReference(增强版)

    概述 JAVA对象引用体系除了强引用之外,出于对性能.可扩展性等方面考虑还特地实现了四种其他引用:SoftReference.WeakReference.PhantomReference.FinalR ...

  10. 宅在家学不进去吗?试试这些 GitHub 上简单易学的游戏项目吧

    作者:HelloGitHub-小鱼干 这是本人宅在家里的第 4 周,代码不想看,技术文章不想读,都不能愉快学习了我还怎么当一个优秀的需求消化师呢?有没有什么轻松地方法来学习技术呢?想起了小时候金山打字 ...