【LeetCode】解题85:Maximal Rectangle

LeetCode刷题:85. Maximal Rectangle LeetCode刷题:85. Maximal Rectangle 原题链接:https://leetcode.com/problems/maximal-rectangle/ Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its... 阅读详情

Problem 85: Maximal Rectangle [Hard]

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

Example:

Input:
[
[“1”,“0”,“1”,“0”,“0”],
[“1”,“0”,“1”,“1”,“1”],
[“1”,“1”,“1”,“1”,“1”],
[“1”,“0”,“0”,“1”,“0”]
]
Output: 6

来源:LeetCode

解题思路

本题需要沿用题解84:Largest Rectangle in Histogram的思想,相当于是对84题的二维扩展。
一个二维矩阵中的最大的全1面积,可以分解为对每一行求一次Largest Rectangle in Histogram,最后对m行结果取最大值。其中每行的histogram是该行的每一列往上能够延伸的最高全1高度。

具体过程:

  • 首先遍历数组,对当前行i求取一个hist,求取公式为:
    h i s t [ j ] = ( h i s t [ j ] + 1 ) ∗ m a t r i x [ i ] [ j ] , j ∈ [ 0 , n − 1 ] hist[j] = (hist[j] + 1) * matrix[i][j], j\in [0, n-1] hist[j]=(hist[j]+1)∗matrix[i][j],j∈[0,n−1]
    意义是对上一行的hist[j]累计1,若当前值matrix[i][j]是0时则清零。其中n为数组的列数。
  • 然后对i行的hist求Largest Rectangle in Histogram,这里使用栈的方法,具体过程见题解84:Largest Rectangle in Histogram.
  • 使用返回的area更新最大面积maxArea。

整个算法遍历了m行,每行使用一次Largest Rectangle in Histogram计算复杂度为 O ( n ) O(n) O(n),因此最终的时间复杂度为 O ( m n ) O(mn) O(mn)。

Solution (Java)

class Solution {
    int m, n;
    public int maximalRectangle(char[][] matrix) {
        m = matrix.length;
        if(m == 0) return 0;
        n = matrix[0].length;
        int[] hist = new int[n];
        int area, maxArea = 0;
        for(int i = 0; i < m; i++){
            for(int j = 0; j < n; j++){
                hist[j] = (hist[j] + 1) * Integer.parseInt(String.valueOf(matrix[i][j]));
            }
            area = largestRect(hist);
            maxArea = Math.max(maxArea, area);
        }
        return maxArea;
    }
    private int largestRect(int[] hist){
        Stack<Integer> sk = new Stack<Integer>();
        sk.push(-1);
        sk.push(0);
        int area, max = 0;
        for(int i = 1; i < n; i++){
            while(i < n && hist[i] >= hist[sk.peek()]){
                sk.push(i);
                i++;
            }
            if(i == n) break;
            while(sk.peek() != -1 && hist[i] <= hist[sk.peek()]){
                area = hist[sk.pop()] * (i - sk.peek() - 1);
                max = Math.max(max, area);
            }
            sk.push(i);
        }
        while(sk.peek() != -1){
            area = hist[sk.pop()] * (n - sk.peek() - 1);
            max = Math.max(max, area);
        }
        return max;
    }
}
Leetcode_85题:Maximal_Rectangle题解报告(C++) 原题网址:https://leetcode.com/problems/maximal-rectangle/description/题目描述:Given a 2D binary matrix filled with 0’s and 1’s, find the largestrectangle containing only 1’s and return its area.For example, g... 阅读详情

相关推荐

Oracle 19c RAC安装踩坑实录:OpenSSH 8.x版本与OUI的“兼容性战争”及一劳永逸的解决思路

本文深入解析Oracle 19c RAC在Linux8环境中部署时遇到的OpenSSH 8.x版本兼容性问题,特别是[INS-06006]错误的根源与解决方案。从协议层分析冲突本质,提供SCP包装器技术、官方补丁应用指南及现代化部署体系构建三种解决方案,帮助企业高效完成RAC安装并规避类似问题。

weixin_42648844的博客 456

leetcode 85. Maximal Rectangle

public int maximalRectangle(char[][] matrix) { if(matrix == null || matrix.length==0|| matrix[0].length==0) return 0;//记住逻辑或和逻辑与有短路的,要先判断if条件的优先级 int n = matrix.length;//行数

u112222222的博客 380

airodump-ng、aircrack-ng使用教程

wifi破解、airodump-ng、aircrack-ng脚本

ssmbyss的博客 2231

leetcode解题报告: 85 Maximal Rectangle

问题描述:给定一个2维位矩阵,元素是0或者1。给出最大的全0矩阵大面积。 转载于:https://blog.51cto.com/koala87/1627320

weixin_34346099的博客 127

西门子Simotion运动控制

SIMOTION

rjimhcd的博客 3023

Flink项目基础配置指南

在大数据处理领域,Apache Flink凭借强大的实时流处理和批处理能力,成为众多开发者的首选工具。在日常工作中,开发Flink Jar任务是常见需求,但每次都需重复配置日志、梳理pom依赖、设置打包插件等,流程繁琐且易出错。为提升开发效率,减少重复劳动,将这些基础配置进行整理归纳十分必要。本文将围绕Flink项目的本地日志配置、pom依赖及插件配置展开详细介绍,为开发者提供一套可直接复用的基础配置方案,助力Flink项目高效开发。在Flink项目中,日志是了解任务运行状态、排查问题的重要依据。本地日志配

qq_42773076的博客 1210

Leetcode 85. Maximal Rectangle 最大矩形 解题报告

1 解题思想这道题我是转化成上一道题来做的,对于每一行,看成给一个直方图在每一行上,首先按照直方图的方式进行统计,然后记录并更新最大,因此计算请看: Leetcode 84. Largest Rectangle in Histogram 最大矩形 解题报告2 原题Given a 2D binary matrix filled with 0’s and 1’s, find the largest

MebiuW的专栏 2810

实战分享:如何用Dify和MaxKb实现文档智能切分与高效检索(附代码解析)

本文详细介绍了如何结合Dify和MaxKB构建文档智能切分与高效检索系统。针对传统全文检索的不足,提出了结构感知的文档切分方案,通过解析文档层级关系并构建标题链,实现了精准的章节级检索。文章包含完整的代码实现,涵盖文档解析、智能切分、向量化策略及双索引优化,并分享了性能调优和生产环境实践经验。

qsc90123456的博客 543

leetcode:Maximal Rectangle

这是leetcode的第85题,原题如下:Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones and return its area.leetcode的第84题是Largest Rectangle in Histogram,原题如下: 解决方案在http

不积跬步,无以至千里 1778

辐射制冷无规散射结构非球形散射体的结构优化与设计准则

本文针对辐射制冷材料中非球形散射体的光散射特性开展研究,提出了一种基于体积散射截面和不对称因子的综合评价方法。通过建立简化模型分析球体、圆柱体、长方体等六种几何形状在太阳波段的散射性能,发现金字塔形和圆锥形具有更强的后向散射能力。研究创新性地提出"反射因子SR=S(1-g)"这一评价参数,该参数与宏观反射率高度相关,为非球形散射体的优化设计提供了定量依据。仿真结果表明,合理选择散射体形状可显著提升辐射制冷材料的太阳反射率,为实现高效被动制冷提供了新的结构设计思路。

huanghm88的专栏 77

ardupilot 日志分析《xKF1信息》

ardupilot 日志分析系列将会把ardupilot所有的日志文档进行分析,为我们分析无人机的运行状态,炸鸡原因,传感器状态,电机状态,控制信息,滤波信息做准备。本节主要分析xKF1日志信息,欢迎批评指正。学习好ardupilot必须会分析飞行日志,从飞行日志中我们可以学到很多无人机的知识。无人机是综合知识的体现,涵盖:导航、控制、滤波、算法、协议等各种知识,只有更全面的学习,才能在无人机的道路上越走越远。

陌城烟雨 1225

[leetcode] 85. Maximal Rectangle 解题报告

题目链接:https://leetcode.com/problems/maximal-rectangle/ Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 思路:这一题是Maximal Squar

小榕流光的专栏 1546

机器人控制基础:伺服驱动器的泄放电阻

摘要:伺服驱动器泄放电阻用于消耗电机减速或制动时产生的再生电能,防止母线电压过高损坏元件。其核心作用是将多余电能转化为热能,规避硬件损坏和停机风险。选型需关注功率、阻值和封装三大参数,功率应匹配再生能量,阻值需符合驱动器要求,封装应满足散热需求。可通过动态监测母线电压或静态检测电阻阻值来判断其是否正常工作。

start_up_go的博客 380

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

题目分析: 给定一个仅包含 0 和 1 的二维二进制矩阵,找出只包含 1 的最大矩形,并返回其面积。示例: 输入: [ [“1”,“0”,“1”,“0”,“0”], [“1”,“0”,“1”,“1”,“1”], [“1”,“1”,“1”,“1”,“1”], [“1”,“0”,“0”,“1”,“0”] ] 输出: 6 解题思路: 仔细观察会返现这一题其实和上一题可以用一样的解法解出来,先得到每一列的...

L141210113的专栏 371

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

LeetCode 85. Maximal Rectangle 题解

本题是"柱状图中最大的矩形"问题的扩展,需要将二维问题转化为一维问题。将每一行看作柱状图的底部计算每个位置的高度(向上连续的 1 的个数)对每一行使用单调栈求最大矩形面积通过本题可以深入理解如何将复杂问题分解为已解决的子问题。

cannonjinx的博客 4093

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

LeetCode-85. Maximal Rectangle [C++][Java]

Given a binary filled with 's and 's, find the largest rectangle containing only 's and return its area.

贫道绝缘子的博客 620

华为的C语言编程规范.pdf

华为技术有限公司内部技术规范。本规范制定了编写C语言程序的基本原则、规则和建议。从代码的清晰、简洁、可测试、安全、程序效率、可移植各个方面对C语言编程作出了具体指导。

LeetCode 85. Maximal Rectangle

https://leetcode.com/problems/maximal-rectangle/ Description Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area. Example: Input: [ [...

pku_Coder的博客 333

Leetcode-85. Maximal Rectangle

公众号不定期发布一篇关于Leetcode解题技巧的文章,尝试从多角度、不同方法对题目进行解析。欢迎大家关注! 题目描述(困难难度) 给一个只有 0 和 1 的矩阵,输出一个最大的矩形的面积,这个矩形里边只含有 1。 解法一 暴力破解 参考这里,遍历每个点,求以这个点为矩阵右下角的所有矩阵面积。如下图的两个例子,橙色是当前遍历的点,然后虚线框圈出的矩阵是其中一个矩阵。 怎么找出这样的矩阵呢?如...

猫敷雪 555

LeetCode85 Maximal Rectangle java题解

public static int maximalRectangle(char[][] matrix) { int rowNum=matrix.length; if(rowNum==0) return 0; int columnNum=matrix[0].length; int[][] height=new int[rowNum][colum

u012249528的专栏 1395

【LeetCode】 85. Maximal Rectangle 最大矩形(Hard)(JAVA)

【LeetCode】 85. Maximal Rectangle 最大矩形(Hard)(JAVA) 题目地址: https://leetcode.com/problems/maximal-rectangle/ 题目描述: Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing o...

qq_16927853的博客 232

LeetCode85 Maximal Rectangle 解题报告

Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1...

bemf168的博客 276

[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 return its area. For example, given the following matrix: 1 0 1 0 0 1 0 1 1 1 1 1 1 1

魔豆(Magicbean)的博客 3149

【LeetCode】 85. Maximal Rectangle 最大矩形(Hard)(JAVA)每日一题

【LeetCode】 85. Maximal Rectangle 最大矩形(Hard)(JAVA) 题目地址: https://leetcode.com/problems/maximal-rectangle/ 题目描述: Given a rows x cols binary matrix filled with 0’s and 1’s, find the largest rectangle containing only 1’s and return its area. Example 1: I

qq_16927853的博客 229

LeetCode_Array_85. Maximal Rectangle最大矩形(C++)【单调栈】

1,题目描述 英文描述 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area. Example: Input: [ ["1","0","1","0","0"], ["1","0","1","1","1"], ["1","1","1","1","1"], ["1","0","0","1","0"] ] ...

&再见萤火虫&的博客 299

85. Maximal Rectangle

LeetCode题目:85. Maximal Rectangle 原题链接:https://leetcode.com/problems/maximal-rectangle/description/ 解题思路:核心思想: 84题的延伸 可以将每一行,看成一个直方图,值为0,代表直方图为0,值为1,代表直方图为n(n为该点向上数连续为1的个数)。举个例子,例图中转换的直方图为:第一行:[1, 0,

MyCodecOdecoDecodE的博客 287
上一篇: 【LeetCode】解题84:Largest Rectangle in Histogram
下一篇: 【LeetCode】解题90:Subsets II
Fayedy
博客等级 码龄11年 2粉丝 57原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值