LeetCode OJ: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.

求出0,1矩阵中的最大矩形区域:

DP问题,可以使用数组记下当前行位置之前的所有1的个数,然后用一个三重循环来找以(i,j)为右下角的矩形的最大的面积,比较得到最大值。这样做复杂度还是比较高的。但是胜在简单,代码如下所示:

 1 class Solution {
 2 public:
 3     int maximalRectangle(vector<vector<char>>& matrix) {
 4         if(matrix.size() == 0 || matrix[0].size() == 0)
 5             return 0;
 6         int szRow = matrix.size();
 7         int szCol = matrix[0].size();
 8         vector<vector<int>> dp(szRow, vector<int>(szCol, 0));//表示当前行前面含有的1的个数
 9         for(int i = 0; i < szRow; ++i){ //第一列的每一行的第一个元素
10             dp[i][0] = matrix[i][0] == ‘1‘ ? 1 : 0;
11         }
12         for(int i = 0; i < szRow; ++i){ //后面每列的每行的每个元素
13             for(int j = 1; j < szCol; ++j){
14                 dp[i][j] = matrix[i][j] == ‘1‘ ? dp[i][j-1]+1 : 0;
15             }
16         }
17         int maxVal = 0;
18         for(int i = 0; i < szRow; ++i){
19             for(int j = 0; j < szCol; ++j){
20                 int width = INT_MAX;
21                 for(int k = i; k >=0 ; --k){
22                     if(dp[k][j] == 0)
23                         break;
24                     width = min(width, dp[k][j]);//求出每次正方形的宽度
25                     maxVal = max(maxVal, width * (i - k + 1));//宽度乘以高度
26                 }
27             }
28         }
29         return maxVal;
30     }
31 };

先马一下别人写的方法,感觉写的挺好的,有时间在来写一下

时间: 2024-10-21 06:23:00

LeetCode OJ:Maximal Rectangle(最大矩形)的相关文章

[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. Example: Input: [ ["1","0","1","0","0"], ["1","0","1",&qu

[LeetCode OJ] Largest Rectangle in Histogram

Given n non-negative integers representing the histogram's bar height where the width of each bar is 1, find the area of largest rectangle in the histogram. Above is a histogram where width of each bar is 1, given height = [2,1,5,6,2,3]. The largest

【leetcode】Maximal Rectangle

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. 使用dpHeight[]数组来记录到第i行为止,第j个位置垂直连续包含多少个1(包括matrxi[i][j]).如: 1 0 1 1 0 1 1 0 1 0 0 1 1 1 1 有如下结果: 第1行: dpHeight[] =

[LeetCode] 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. 此题是之前那道的Largest Rectangle in Histogram 直方图中最大的矩形 的扩展,这道题的二维矩阵每一层向上都可以看做一个直方图,输入矩阵有多少行,就可以形成多少个直方图,对每个直方图都调用Largest Rectangle in Hist

LeetCode (85): Maximal Rectangle [含84题分析]

链接: 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. [中文描述] 给一个二维数组, 算出里面最大的全1矩形面积,比如: [ ['1','1','1','0'], ['1','1','1','1']

【leetcode】Maximal Rectangle (hard)★

Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ones and return its area. 找到01矩形中最大的全1子矩阵. 我自己的思路: 我先用一个跟输入相同大小的矩阵numInCol 存储从当前位置开始向下有多少连续的1. 如 1 0 1 0 1 1 1 1 1 其numInCol 是 1 0 3 0 2 2 1 1 1 然后用一个变量tmpans

LeetCode OJ 223.Rectangle Area

Find the total area covered by two rectilinear rectangles in a 2D plane. Each rectangle is defined by its bottom left corner and top right corner as shown in the figure. Assume that the total area is never beyond the maximum possible value of int. 对于

leetCode 85.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. 思路:此题的意思是给一个为0或1的矩阵,求所有为1组成的最大矩阵的面积. 此题能够巧妙转化为求最大直方图面积的问题. public class Solution { //其思想是将每一列的1逐行相加,遇0为0.遇1相加 //然后转化为求每一行的最大直方图面积的求解

19.2.23 [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. Example: Input: [ ["1","0","1","0","0"], ["1","0","1",&qu

LeetCode 85. Maximal Rectangle

1 class Solution { 2 public: 3 int maximalRectangle(vector<vector<char>>& matrix) { 4 /** largest rectangle based solution **/ 5 if(matrix.size()<=0 || matrix[0].size()<=0) 6 return 0; 7 int m=matrix.size(); 8 int n=matrix[0].size()+