一、题目说明

题目,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. JS绘图

    https://www.highcharts.com.cn/demo/highcharts/ 百度的Echarts https://www.echartsjs.com/zh/index.html

  2. Java入门 - 语言基础 - 07.修饰符

    原文地址:http://www.work100.net/training/java-modifier-type.html 更多教程:光束云 - 免费课程 修饰符 序号 文内章节 视频 1 概述 2 访 ...

  3. 玩转Django2.0---Django笔记建站基础十三(第三方功能应用)

    第13章 第三方功能应用 在前面的章节中,我们主要讲述Django框架的内置功能以及使用方法,而本章主要讲述Django的第三方功能应用以及使用方法.通过本章的学习,读者能够在网站开发过程中快速开发网 ...

  4. 生成URL(而不是链接) Generating URLs (and Not Links) | 在视图中生成输出URL |高级路由特性 | 精通ASP-NET-MVC-5-弗瑞曼

    结果呢:

  5. LeetCode刷题总结-链表

    LeetCode刷题总结-链表 一.链表     链表分为单向链表.单向循环链表和双向链表,一下以单向链表为例实现单向链表的节点实现和单链表的基本操作. 单向链表 单向链表也叫单链表,是链表中最简单的 ...

  6. Git详解之特殊工具

    前言 现在,你已经学习了管理或者维护 Git 仓库,实现代码控制所需的大多数日常命令和工作流程.你已经完成了跟踪和提交文件的基本任务,并且发挥了暂存区和轻量级的特性分支及合并的威力. 接下来你将领略到 ...

  7. servlet 深入了解

    servlet  作用 在Java web b/s架构中,servlet扮演了重要的角色,作为一个中转处理的容器,他连接了客户端和服务器端的信息交互和处理.简单来说,客户端发送请求,传递到servle ...

  8. Mybatisplus代码生成器主类CodeGenerator配置

    //代码自动生成public class CodeGenerator { /** * <p> * 读取控制台内容 * </p> */ public static String ...

  9. illegal use of this type as an expression

    学习MCI时看别人样例手敲代码出现的一个很经典的错误. 在C语言中定义的变量没有放在函数的开头. #include <string.h> #include <windows.h> ...

  10. Mysql:自动化备份

    简介 在这个数据为王的时代,数据的备份十分重要,这里就分享一篇mysql数据库自动备份的脚本(是从网上搜到的),其将配置文件和备份脚本分离,提高了安全性,脚本风格规范严谨,分享给大家希望对需要的小伙伴 ...