版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/seagal890/article/details/89072523
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 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
题目的大意是:在一个给定的二维矩阵中,数组元素由0或者1组成,要找出一个由1构成的最大矩形使得面积最大。
在解答这道题目是,网上很多解法都参考了 LeetCode 84 题的解法。
LeetCode 84. Largest Rectangle in Histogram https://leetcode.com/problems/largest-rectangle-in-histogram/
解题:https://blog.csdn.net/seagal890/article/details/89074836
本题的解法:
// 首先,也是用dp做:heights保存某一列从上数第i行的连续的'1'的个数;
// 然后使用84的逻辑,取连续区间最大的矩形大小就可以了;
public int maximalRectangle(char[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0] == null || matrix[0].length == 0) return 0;
int width = matrix[0].length, res = 0;
int[] heights = new int[width];
for (char[] row : matrix) {
for (int c = 0; c < width; c++) {
if (row[c] == '1') heights[c]++;
else heights[c] = 0;
}
res = Math.max(res, largestRectangleArea(heights));
}
return res;
}
// 以下部分是第84题的解法:分治求最大矩形(不需要修改)
// 思路:对于每一段区间,都存在一个最小值
// 对于最小值,无非就是三种可能:
// 1:要么整段面积最大,2、3:要么最小值左边或者最小值右边(均不包含最小值)的面积最大,采用分治法递归解决;
// 遇到有序排列的区间,采用递归会降低效率,于是只要单独计算并且比较就可以
public int largestRectangleArea(int[] heights) {
return largestRect(heights, 0, heights.length - 1);
}
private int largestRect(int[] heights, int start, int end) {
if (start > end) return 0;
if (start == end) return heights[start];
int minIndex = start;
// 使用是否有序排列的变量可以显著提高效率
// 这里可以检测双向(变大或者变小的顺序)
int inc = 0, dec = 0;
for (int i = start + 1; i <= end; i++) {
if (heights[i] < heights[minIndex]) minIndex = i;
if (heights[i] > heights[i - 1]) inc++; // 升序
else if (heights[i] < heights[i - 1]) dec--; // 降序
}
int res = 0;
// 升序
if (dec == 0) {
for (int i = start; i <= end; i++)
res = Math.max(res, heights[i] * (end - i + 1));
} // 降序
else if (inc == 0) {
for (int i = start; i <= end; i++)
res = Math.max(res, heights[i] * (i - start + 1));
} // 无序
else {
res = Math.max(Math.max(largestRect(heights, minIndex + 1, end), largestRect(heights, start, minIndex - 1)),
heights[minIndex] * (end - start + 1));
}
return res;
}
另外一种解法:
public int MaximalRectangle(char[,] matrix) {
// Heights accum
int[] dp = new int[matrix.GetLength(1)];
int max = 0;
for(int i = 0; i < matrix.GetLength(0); ++i)
{
// Accumlate heights for current row
for(int j = 0; j < matrix.GetLength(1); ++j)
{
dp[j] = matrix[i,j] == '1'? dp[j] + 1 : 0;
}
// Finding maximum rectangle in current state, O(columns)
max = Math.Max(max, LargestRectangleArea(dp));
}
return max;
}
// Largest rectangle in historgram, leet code #84
int LargestRectangleArea(int[] heights)
{
Stack<int> st = new Stack<int>();
int left = 0;
int max = 0;
while (left <= heights.Length)
{
while (st.Count > 0 && (left == heights.Length || heights[st.Peek()] > heights[left]))
{
var h = heights[st.Pop()];
if (st.Count == 0) max = Math.Max(max, h * left);
else max = Math.Max(max, h * (left - st.Peek() - 1));
}
st.Push(left++);
}
return max;
}