[LeetCode]Maximal Rectangle

计算矩阵中最大矩形面积
本文介绍了一种计算二维矩阵中包含连续1的最大矩形面积的方法,通过维护高度、左边界和右边界来实现最优解。
class Solution {
//for each element in matrix, we compute h[j](current consecutive length of '1'), L[j] (nearest left wall whose height is smaller than current column), and R[j] (nearest right wall whose height is smaller than current column) for it, update these three terms row by row
//here we compute the most far range(both in height, and width) of scan line j at every row i
//to get the size of the rectangle of f[i][j]. To achieve this, we should compute:
//H[j](the maximum bottom up height of current column until meet the first '0')
//L[j](the most far position where can the scan line j can go at the left)
//R[j](the most far position where can the scan line j can go at the right)
//then f[i][j]=H[j]*(R[j]-L[j]+1), ans=max(ans,f[i][j])
public:
	int maximalRectangle(vector<vector<char> > &matrix) {
		// Start typing your C/C++ solution below
		// DO NOT write int main() function
		int ans = 0;
		if(matrix.size() == 0) return ans;
		int m = matrix[0].size();
		vector<int>L(m, -1);
		vector<int>R(m, m);
		vector<int>H(m, 0);
		for (int i = 0; i < matrix.size(); ++i)
		{
			//scan from left to right to update H and L
			int nearestLeft = -1;//virtual '0' at most far of right
			for (int j = 0; j < matrix[i].size(); ++j)
			{
				L[j] = max(nearestLeft, L[j]);
				if (matrix[i][j] == '1')
					H[j]++;
				else
				{
					H[j] = 0;
					L[j] = -1;//note here
					nearestLeft = j;
				}
			}
			//scan from right to left to update R and calculate f[i][j]
			int nearestRight = m;//virtual '0' at most far of left
			for (int j = matrix[i].size()-1; j >= 0; --j)
			{
				R[j] = min(nearestRight, R[j]);
				if (matrix[i][j] == '0')
				{
					nearestRight = j;
					R[j] = m;//note here
				}
				//calculate f[i][j]
				ans = max( ans, H[j]*(R[j]-L[j]-1) );
			}
			
		}
		return ans;
	}
};

在这段代码中,`// 主函数:求01矩阵中最大全1矩形面积` 是对下方函数 `int maximalRectangle(vector<vector<int>>& matrix)` 的注释说明。我们重点分析的是这行代码: ```cpp int maximalRectangle(vector<vector<int>>& matrix) { ``` ### 功能和作用分析 这行代码定义了一个名为 `maximalRectangle` 的函数,它是解决“在给定的 01 矩阵中寻找最大全 1 子矩形面积”问题的核心函数。以下是其详细功能与作用解析: --- #### ✅ 函数名称:`maximalRectangle` - **语义含义**:寻找最大矩形(通常是面积最大的由 '1' 构成的矩形)。 - 这是一个经典算法题(LeetCode #85: Maximal Rectangle),常用于图像处理、动态规划与栈的应用场景。 #### ✅ 参数类型:`vector<vector<int>>& matrix` - 表示一个二维整数矩阵,元素为 0 或 1。 - 使用 **引用传递**(`&`)避免拷贝整个二维数组,提高效率。 - 输入形式如: ``` 1 0 1 0 0 1 0 1 1 1 1 1 1 1 1 1 0 0 1 0 ``` #### ✅ 返回值类型:`int` - 返回一个整数,表示能构成的最大全 1 矩形的面积(单位格子数)。 #### ✅ 核心思想(结合后续代码理解) 该函数通过将二维问题转化为多个一维“柱状图中最大矩形”问题来求解: 1. 维护一个一维数组 `heights`,记录每一列从当前行向上连续 '1' 的高度。 2. 对每一行更新 `heights[]` 数组。 3. 调用 `largestRectangleInHistogram(heights)` 计算以当前行为底边时所能形成的最大矩形面积。 4. 遍历所有行后取最大值作为结果。 > 因此,`maximalRectangle` 实际上是利用了“柱状图最大矩形”这一子问题(单调栈方法)进行逐层扫描,从而解决二维矩阵中的最大矩形问题。 #### ✅ 在整体程序中的角色 - 是主逻辑入口函数之一(被 `main()` 调用)。 - 将原始的二维矩阵问题降维成一系列一维问题,复用 `largestRectangleInHistogram` 函数实现高效求解。 - 属于典型的 **动态规划 + 单调栈** 结合应用。 --- ### 总结 ```cpp int maximalRectangle(vector<vector<int>>& matrix) ``` 这行代码定义了一个关键函数,其作用是: > **接收一个 01 矩阵,计算其中由 '1' 构成的最大矩形区域的面积**。 它通过逐行构建“柱子高度”,并调用基于单调栈优化的一维最大矩形算法,最终返回全局最大面积。 该函数是整个程序解决问题的核心驱动部分。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI记忆

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值