题目描述:

给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。

思路分析:

这题是之前那道最大正方形的进阶,同样是利用dp来求解。通过逐行去计算最大矩形,即优化的子结构是当前行的最大矩形,从1行到2行,最后到n行。需要利用三个数组来求矩形面积,首先是h,表示当前位置矩形的最大高度,l和r分别表示了当前位置可向左右延伸到多远。其中l数组存左边界,r数组存右边界,是一个左闭右开区间。当遇到‘1’时,需要更新左右边界,左边界的更新是l[i] = max(l[i], cur_left),这里的l[i]即上一行时在当前位置的左边界,cur_left则表示在当前行的左边界,以此来保证其延伸的区域不会低于当前位置的高度。右边界的更新为r[i] = min(r[i], cur_right),右边界需要从右往左。

代码:

 class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int row = matrix.size();
if(row == )
return ;
int col = matrix[].size();
vector<int>l(col, );
vector<int>r(col, col);
vector<int>h(col, );
int max_area = ;
for(int i=; i<row; i++)
{
int cur_left=, cur_right=col;
for(int j=; j<col; j++)
{
if(matrix[i][j]=='')
{
h[j] = h[j]+;
}
else
h[j] = ;
}
for(int j=; j<col; j++)
{
if(matrix[i][j]=='')
{
l[j] = max(l[j], cur_left);
}
else
{
cur_left = j+;
l[j]=;
}
}
for(int j=col-; j>=; j--)
{
if(matrix[i][j]=='')
{
r[j] = min(r[j], cur_right);
}
else
{
cur_right = j;
r[j] = col;
}
}
for(int j=; j<col; j++)
{
max_area = max(max_area, (r[j]-l[j])*h[j]);
}
}
return max_area;
}
};

leetcode 85. 最大矩形的更多相关文章

  1. Java实现 LeetCode 85 最大矩形

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

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

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

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

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

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

  5. 【LeetCode 85】最大矩形

    题目链接 [题解] 把所有的"1"矩形分成m类. 第j类的矩形.他的右边界跟第j列紧靠. 那么. 我们设f[i][j]表示(i,j)这个点往左最长能延伸多少个数目的"1& ...

  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. FreePascal - CodeTyphon 如何让编译的程序更小!

    CodeTyphon 6.9 在菜单[project]-->[project option]的弹出界面中 选择[compiler option]-->[debugging] 1,去掉“Ge ...

  2. 转摘jemeter学习-连接数据库之jdbc请求

    JMETER连接数据库 mysql下载地址:https://dev.mysql.com/downloads/connector/j/ mysql连接器根据语言选择/J,用的是Mac,选择下载.tar. ...

  3. vue element-ui tree 根节点固定子节点懒加载 首次加载根节点并展开

    关键代码: <el-tree ref="foldTree" node-key="id" :highlight-current="true&quo ...

  4. 第一册:lesson 131.

    原文: Don't be so sure. question:What's the problem about deciding on a holiday. Where are you going t ...

  5. Odoo中的模型继承、视图继承、Qweb模板继承详解

    转载请注明原文地址:https://www.cnblogs.com/ygj0930/p/10826114.html 在实际开发过程中,经常会遇到需要修改Odoo原生逻辑的情况.然而,直接修改Odoo底 ...

  6. PySpark DataFrame 添加自增 ID

    PySpark DataFrame 添加自增 ID 本文原始地址:https://sitoi.cn/posts/62634.html 在用 Spark 处理数据的时候,经常需要给全量数据增加一列自增 ...

  7. Ubuntu 18.10 安装之后做的一点事

    sb_release -c //查看系统代号 #更新源/etc/apt/sources.list //打开更新目录 deb https://linux.xidian.edu.cn/mirrors/ub ...

  8. Cobbler--自动化部署

    Cobbler自动化部署 cobbler简介 Cobbler 可以用来快速建立 Linux 网络安装环境,它已将Linux网络安装的技术门槛,从大专以上文化水平,成功降低到了初中水平,连补鞋匠都能学会 ...

  9. 发送短信验证码的JAVA代码

    package com.moretickets.platform; import com.alibaba.fastjson.JSONException; import com.alibaba.fast ...

  10. Feign 报错:The bean 'service-producer.FeignClientSpecification', defined in null, could not be registered. A bean with that name has already been defined in null and overriding is disabled.

    报错: 2019-09-17 20:34:47.635 ERROR 59509 --- [ main] o.s.b.d.LoggingFailureAnalysisReporter : ******* ...