刷题85. Maximal Rectangle
一、题目说明
题目,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的更多相关文章
- 85. Maximal Rectangle
85. Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle c ...
- LeetCode (85): Maximal Rectangle [含84题分析]
链接: https://leetcode.com/problems/maximal-rectangle/ [描述] Given a 2D binary matrix filled with '0's ...
- 求解最大矩形面积 — leetcode 85. Maximal Rectangle
之前切了道求解最大正方形的题,题解猛戳 这里.这道题 Maximal Rectangle 题意与之类似,但是解法完全不一样. 先来看这道题 Largest Rectangle in Histogram ...
- 【leetcode】85. Maximal Rectangle(单调栈)
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing onl ...
- 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 ...
- 【LeetCode】85. Maximal Rectangle
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- 85. Maximal Rectangle 由1拼出的最大矩形
[抄题]: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...
- [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 ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
随机推荐
- Ubuntu中部署Django项目的配置与链接MySQL
Django的简介 MVT模式的介绍创建项目的虚拟环境 本次使用的是pip安装 一.更新 sudo apt update 二.安装pip sudo apt install python3-pip 三. ...
- Stream排序Map集合
最近小编自己一个人在负责一个项目的后台开发,其中有一部分是统计相关的功能,所以需要一些排序或者分组的操作,之前这种操作小编觉得还是比较麻烦的,虽热有一些现成的工具类,但是工具类的写法也是比较复杂的,但 ...
- shell脚本快速配置yum源
我们在使用Red Hat系列的Linux系统时经常要配置yum源,本文档提出一个快速配置yum源的方法,就是用shell脚本来实现. 我们在使用Red Hat系列的Linux系统时经常要配置yum源, ...
- GitHub Top 微信小程序——在家中憋了几天写点代码吧
GitHub Top 本项目为 GitHub 热点项目微信小程序客户端,首页仅推荐一个热点项目,这个项目往往是社会热门事件所催生的一个项目,如 996.ICU.wuhan2020,所推荐项目标准为:积 ...
- 一个注解搞懂 Sentinel,@SentinelResource 总结
在前面的博客中,我给大家演示了使用 @SentinelResource 定义资源完成限流的例子, 下面就从源码解析开始,看下SentinelResource是如何实现限流的,以及@SentinelRe ...
- RocketMQ消息模型
rocketmq采用的是发布-订阅的模式,不需要每个消费者维护自己的消息队列,生产者将消息发送到topic,消费者订阅此topic 读取消息. 基本概念: 消息模型:消息模型包括producer,co ...
- JSTL (标准标签库)
JSTL(标准标签库) 作用: Web程序员能够利用JSTL和EL来开发Web程序,取代传统直接在页面上嵌入Java程序(Scripting)的做法,以提高程序的阅读性.维护性和方便性. 使用方法:J ...
- JDK源码之String类解析
一 概述 String由final修饰,是不可变类,即String对象也是不可变对象.这意味着当修改一个String对象的内容时,JVM不会改变原来的对象,而是生成一个新的String对象 主要考虑以 ...
- 什么是ip地址、子网掩码、网关和DNS?
什么是ip地址? IP是32位二进制数据,通常以十进制表示,并以“.”分隔.IP地址是一种逻辑地地址,用来标识网络中一个个主机,IP有唯一性,即每台机器的IP在全世界是唯一的. IP地址=网络地址+主 ...
- DOCKER 学习笔记4 认识DockerCompose 多容器编排
前言 通过上一节的学习,学会了如何在Linux 环境下搭建Docker并且部署Springboot 项目,并且成功的跑了起来,当然,在生产环境中,不只是需要一个后端的Web 项目,还需要比如 Ngin ...