求全是1的最大矩阵面积 Maximal Rectangle @LeetCode

LeetCode(085)- 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. Example: Input: [ [“1”,“0”,“1”,“0”,“0”], [“1”,“0”,“1”,“1”,“1”], [“1”,“1”,“1”,“... 阅读详情

一开始参考http://www.geeksforgeeks.org/maximum-size-sub-matrix-with-all-1s-in-a-binary-matrix/,但发现它的条件是square submatrix,所以本题不符合。

后来参考http://yucoding.blogspot.com/2013/01/incomplete-leetcode-question-47-maximal.html 写的很详细了,转抄于此:


Analysis:

This is not an easy problem and the idea is not straightforward.
Since the question requires the area of all ones region, first we can define the region (rectangle) in the matrix. Any region in the matrix has several basic properties:    1 corner point,  width, and length.
Also which corner point it is decide the exact place of the region, here we consider the bottom-right corner point, because we usually scan the matrix from (0,0) to right and down direction. For this problem, we also need to consider the "all ones" requirement, where all the regions are filled with 1s.

We can then have this data structure:
One 2D array(vector) ones[i][j],  where ones[i][j] stores the number of contiguous 1s ended at matrix[i][j], along ith row. e.g.  if matrix[i] = "1011101", then ones[i]=1,0,1,2,3,0,1. This array stores the length of the rectangle, for every possible bottom-right corner point.

Having this two values, next is to find the height of the rectangle, as well as keep the "all ones" requirement.
Start with a corner point [i][j],  since it is the bottom right corner, the rectangle search direction is left and up.
For the left we already have the number of contiguous 1s ones[i][j]. How to get the height?  We need to scan all the values above ones[i][j], it is from i-1 to 0. If meets 0, then stop, else compute the possible rectangle area, and store the max. To compute the area, the minimum length of rectangle should be compared and stores every time.


package Level5;

/**
 * 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.
 *
 */
public class S85 {

	public static void main(String[] args) {
		char[][] matrix = {{'0','1'},{'0','1'}};
		System.out.println(maximalRectangle(matrix));
	}
	
	public static int maximalRectangle(char[][] matrix) {
		int rows = matrix.length;
		if(rows == 0){
			return 0;
		}
		int cols = matrix[0].length;
		// 先计算全1矩阵的宽度
        int[][] hOnes = new int[rows][cols];		// horizontal ones
        
        int max = 0;	// 最大面积
        for(int i=0; i<rows; i++){
        	for(int j=0; j<cols; j++){
        		if(matrix[i][j] == '1'){
        			if(j == 0){	// 特殊处理左起第一个
        				hOnes[i][j] = 1;
        			}else{
        				hOnes[i][j] = hOnes[i][j-1] + 1;
        			}
        		}
        	}
        }
        
        for(int i=0; i<rows; i++){
        	for(int j=0; j<cols; j++){
        		if(hOnes[i][j] != 0){
        			int minI = i;	// 计算高度,minI不断向上走
        			int minRowWidth = hOnes[i][j];	// 随着向上走,宽度也要随之调整
        			while(minI >= 0){
        				minRowWidth = Math.min(minRowWidth, hOnes[minI][j]);
        				int area = minRowWidth * (i-minI+1);
        				max = Math.max(max, area);
        				minI--;
        			}
        		}
        	}
        }
        
        return max;
    }

}


在一个只包含0,1的二维数组中找到最大矩形的面积 1、用一个height数组表示从从上到下连续的1的高度。left,right表示当前矩形的边界。maxA = max(maxA,(right[j]-left[j])*height[j]); 例如: 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1 1 0对第一行: l: 0 0 0 3 0 0 0 r: 7 7 7 4 7 7 7 对第二行: l: 0 阅读详情

相关推荐

求一矩阵中子矩阵的最大和

35. 求一个矩阵中最大的二维矩阵(元素和最大).如: 1 2 0 3 4 2 3 4 5 1 1 1 5 3 0 中最大的是: 4 5 5 3 要求:(1)写出算法;(2)分析时间复杂度;

LeetCode: Maximal Rectangle

Maximum size square sub-matrix with all 1s http://www.geeksforgeeks.org/maximum-size-sub-matrix-with-all-1s-in-a-binary-matrix/ 1 public static int maximalRectangle(char[][] matrix) { 2 ...

weixin_30454481的博客 120

基于STM32的工业I/O模块硬件电路实现

深入解析基于STM32的工业I/O模块硬件电路实现,涵盖关键电路设计与信号处理,突出硬件电路稳定性和工业级可靠性,适合嵌入式开发者参考。

weixin_35835018的博客 456

Arithmetic problem | 找二维矩阵权值为1的最大矩形面积

转载 https://blog.youkuaiyun.com/sweeterer/article/details/52053962 给你一个二维矩阵,权值为False和True,找到一个最大的矩形,使得里面的值全部为True,输出它的面积。                  ...

hengxingheng的博客 527

Amesim(五):单个悬架系统搭建

一、组成 单个悬架系统组成包括:端子,质量块*2,弹簧,阻尼器,速度路面反馈,路面激励。 二、搭建 打开Amesim,在 library 选择 1D Mechanical 选择质量模块,添加到skech中,并按 ctrl+r 旋转质量块,将箭头朝上,设置规定的力传递正方向 添加弹簧阻尼器,与质量块连接 添加VX模块,将路面的激励传递转化为速度和位移,传递给悬架系统 在...

林小川的博客 7170

关于LIO-SAM编译时出现错误 /usr/bin/ld: 找不到 -lBoost::timer

关于LIO-SAM编译时出现错误: 下载LIO-SAM代码 进入LIO-SAM文件夹,通过以下代码编译: mkdir build cd build cmake .. make 报错: /usr/bin/ld: 找不到 -lBoost::timer collect2: error: ld returned 1 exit status CMakeFiles/lio_sam_imuPreintegration.dir/build.make:759: recipe for target 'devel/lib/li

wwtx的博客 3648

矩阵中全为1的矩形面积的最大值

问题描述: 给定一个矩阵,该矩阵中的元素只包含1和0,找出该矩阵中全为1的矩形面积的最大值。 如: 1 1 0 0 0 1 0 0 0 1 1 0 0 0 1 对应的矩形面积的最大值为3。 整体思路: 通过求解每列中到当前行的连续1的个数,然后将该题转化为求解柱状图最大面积问题。 举个栗子: 1 1 0 1 0 1 0 1 1 1 1 0 1 0 1 计算每列中到当前行的连续1的个数得到: 1 ...

x2017x的博客 3860

Visual Studio 命令提示符工具区别与使用场景

Visual Studio会提供一系列命令提示符工具,这些工具应用在哪些场景呢?本文进行了相关介绍。

cicada15的博客 3493

[LeetCode] 01矩阵中最大正方形 Maximal Square

Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 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 0 Re

jiyanfeng1的专栏 9230

【异常】解决JDK21升级中的SecurityException: JCE无法验证BC提供者问题

本文详细解析了JDK21升级中常见的SecurityException: JCE无法验证BC提供者问题的根源与解决方案。核心在于JDK21强化了JCE安全验证,导致签名不合规的老版本Bouncy Castle库被拒绝。文章提供了通过Maven依赖树精准定位、排除旧版本并引入兼容的bcprov-jdk18on新库的完整步骤,以及清理缓存等实战技巧,帮助开发者彻底解决此兼容性问题。

weixin_29197051的博客 491

CVPR 2027 投稿规划:基于近3年录用率与截稿周期的5步时间管理法

本文为计算机视觉研究者提供CVPR 2027投稿的完整规划指南,基于近三年录用率与截稿周期数据,提出五步时间管理法。涵盖选题策略、实验设计、论文写作、同行评审到最终提交的全流程优化,特别针对CVPR日益激烈的竞争环境(2024年投稿量达11532篇,录取率仅23.6%)提供实用解决方案,帮助研究者提升投稿成功率。

weixin_34079177的博客 1161

求一个只包含0、1的矩阵中只包含1的最大子矩阵大小

题目描述: 给定一个无序矩阵,其中只有1和0两种值,求只含有1的最大正方形的大小。 例如给定如下矩阵: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 public class maxRec { public static int get(int []arr){ int max = 0; for(int

u012763142的专栏 8689

YOLO由浅入深,最简单地说明白YOLO基本实现原理

YOLO(You Only Look Once)是一种高效的实时目标检测技术,广泛应用于自动驾驶、监控和无人机等领域。其核心优势在于速度快,能够在视频流中实时识别目标并标出其位置、大小、类别及置信度。YOLO基于卷积神经网络(CNN),通过卷积层提取图像特征,池化层和扁平化层优化特征处理,全连接层进行分类。与传统方法不同,YOLO将图像分割成多个网格,每个网格预测多个边界框及其置信度,仅对高置信度的框进行详细计算,从而快速确定目标的位置和类别。这种机制使得YOLO在保持高准确率的同时,实现了高效的实时处理能

qq_30279827的博客 566

python 输入一个二维01矩阵,判断矩阵中全为1的正方形的最大边长

一,输入一个二维01矩阵,判断矩阵中全为1的正方形的最大边长 1, 问题描述 输入一个二维01矩阵,判断矩阵中全为1的正方形的最大边长 2, 输入: 0 1 1 0 1 0 1 1 1 0 1 1 0 1 1 1 1 0 0 0 0 1 0 1 1 1 0 0 0 0 输出: ...

wodedipang_的博客 2881

纳米尺度仿真软件:Quantum Espresso_(13).非线性光学性质计算

通过 Quantum Espresso,可以高效地计算材料的非线性光学性质,包括二阶和三阶非线性极化率。这些性质在许多现代技术中具有重要的应用价值。在准备输入文件时,需要详细描述系统的晶格结构和原子位置,选择合适的基组和赝势,并设置计算参数和微扰设置。通过优化计算参数和使用并行计算,可以提高计算效率和精度。最后,使用适当的工具进行结果分析和可视化,可以更好地理解材料的非线性光学行为。

kkchenjj的博客 1278

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

LeetCode: 221_Maximal Square | 二维0-1矩阵中计算包含1的最大正方形的面积 | Medium

题目:   Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 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...

weixin_34221276的博客 562

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

在矩阵中寻找最大正方形连续区域

在矩阵中寻找最大正方形连续区域问题描叙 输入一个矩阵M、一个数字k,找出一个最大的正方形连续区域,这个区域里的数字均是k。 ​ 界的思考 对于矩阵M中的每一个元素,要么等于k要么不等于k,要知道这个数的状态,必须有一次比较。一共n个数,所以至少需要n2n^2次比较。故比较次数的下界为Ω(n2)\Omega(n^2),那么是否存在一个O(n2)O(n^2)的

unclerunning的博客 5586

ssm框架搭建

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

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

程序员刷题(2)求矩阵面积 (Python)

class Rectangle(object): width=0 height=0 def __init__(self,width,height): self.width = width self.height = height def getArea(self): return( self.width*self.heig...

weixin_40376834的博客 1121

给一个二值图,求出最大连通区域(可用深度优先和广度优先算法)

这个问题是我在SLAM求职宝典系列D2篇中遗留的问题,因为内容较多现在单独将其列出进行解答。 本篇内容分为四个部分: 目录 (1)二值图 (2)求最大连通区域的算法 (3)代码实现,以及DFS 和BFS (4) OpenCV中连通域的求解(C++ & Python) (1)二值图 二值图像,顾名思义就是图像的亮度值只有两个状态:黑(0)和白(255...

Welcome to the Western World 1万+

LeetCode 221 Medium 最大1矩形面积 Python

def maximalSquare(self, matrix): """ My Method 算法:动规 思路: 用dp[i][j]记录以matrix[i][j]为"1"矩形的右下角的矩形的最大边长 matrix[i][j] == 0 的显然dp[i][j] == 0 对matrix[i][j] == 1的来说,如果...

h 712

求一个矩阵中最大的二维矩阵(元素和最大)

2019独角兽企业重金招聘Python工程师标准>>> ...

weixin_34337265的博客 3481

每日一练——求一个只包含0、1的矩阵中只包含1的最大子矩阵大小

题目描述: 给定一个无序矩阵,其中只有1和0两种值,求只含有1的最大正方形的大小。 例如给定如下矩阵: 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 Return 4 int max_square(vector>& matrix) { int row = matrix.size(); int col = matrix[0

千里之行,始于足下 7953

基于二维矩阵的深搜(dfs)和广搜(bfs)python实现

该二维矩阵不是邻接矩阵,每个结点只和上下左右4个方向的结点有连接。 def bfs(start, matrix): """广搜""" n = len(matrix) queue = [] queue.append(start) vis = [[False for _ in range(n)] for _ in range(n)] vis[start[...

Aiven 2417

java矩阵中的最大数量_[Swift]LeetCode1183. 矩阵中 1 的最大数量 | Maximum Number of Ones...

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★?微信公众号:为敢(WeiGanTechnologies)?博客园地址:山青咏芝(https://www.cnblogs.com/strengthen/)?GitHub地址:https://github.com/strengthen/LeetCode...

weixin_30635707的博客 1411

【一天一道LeetCode】#85. Maximal Rectangle

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder‘s Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a 2D binary matrix filled with 0’s and 1’s, find the largest rectangle containing all ones a

ZeeCoder 2501
上一篇: 判断是否字符串重组 Scramble String @LeetCode
下一篇: 柱状图内最大的矩形面积 Largest Rectangle in Histogram @LeetCode
chiiis
博客等级 码龄14年 422粉丝 429原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值