Article directory
One [topic category]
- matrix
Two [question difficulty]
- difficulty
Three [topic number]
- 85. Largest Rectangle
Four [title description]
- Given a 2D binary matrix of size rows x cols containing only 0s and 1s, find the largest rectangle containing only 1s and return its area.
Five [topic examples]
-
Example 1:
- Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],[ "1", "1", "1", "1", "1"], ["1", "0", "0", "1", "0"]]
- Output: 6
- Explanation: The largest rectangle is shown in the figure above.
-
Example 2:
- Input: matrix = []
- output: 0
-
Example 3:
- Input: matrix = [["0"]]
- output: 0
-
Example 4:
- Input: matrix = [["1"]]
- Output: 1
-
Example 5:
- Input: matrix = [["0","0"]]
- output: 0
Six [topic prompt]
- r o w s = = m a t r i x . l e n g t h rows == matrix.length rows==matrix.length
- c o l s = = m a t r i x [ 0 ] . l e n g t h cols == matrix[0].length cols==matrix[0].length
- 1 < = r o w , c o l s < = 200 1 <= row, cols <= 200 1<=row,cols<=200
- matrix [ i ] [ j ] is ' 0 ' or ' 1 ' matrix[i][j] is '0' or '1'ma t r i x [ i ] [ j ] is′0' or′1′
Seven [problem-solving ideas]
- First create an auxiliary array left to record the number of consecutive '1' on the left of each position
- Then for each point in the two-dimensional array, we calculate the area of the rectangle with this point as the lower right corner, we use the "upward expansion" method, the width of the matrix is the shortest width in the process of "upward expansion", and the height is passed through the current Subtract the traversed position from the position and add one (because the array starts counting from zero)
- Then get the area of the largest rectangle by comparing the maximum
- Finally return the result
Eight 【Time Frequency】
- Time complexity: O ( m 2 n ) O(m^2n)O(m2n), m 、 n m、n m and n are the number of rows and columns of the incoming two-dimensional array respectively
- Space complexity: O ( mn ) O(mn)O ( mn ),m、nm、nm and n are the number of rows and columns of the incoming two-dimensional array respectively
Nine [code implementation]
- Java language version
class Solution {
public int maximalRectangle(char[][] matrix) {
int m = matrix.length;
int n = matrix[0].length;
int[][] left = new int[m][n];
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j++){
if(matrix[i][j] == '1'){
left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
}
}
}
int res = 0;
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j++){
if(matrix[i][j] == '0'){
continue;
}
int width = left[i][j];
int area = width;
for(int k = i - 1;k >= 0;k--){
width = Math.min(width, left[k][j]);
area = Math.max(area,(i - k + 1) * width);
}
res = Math.max(res, area);
}
}
return res;
}
}
- C language version
int maximalRectangle(char** matrix, int matrixSize, int* matrixColSize)
{
int m = matrixSize;
int n = matrixColSize[0];
int** left = (int **)malloc(sizeof(int*) * m);
for(int i = 0;i < m;i++)
{
left[i] = (int*)calloc(n, sizeof(int));
}
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
{
if(matrix[i][j] == '1')
{
left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
}
}
}
int res = 0;
for(int i = 0;i < m;i++)
{
for(int j = 0;j < n;j++)
{
if(matrix[i][j] == '0')
{
continue;
}
int width = left[i][j];
int area = width;
for(int k = i - 1;k >= 0;k--)
{
width = fmin(width, left[k][j]);
area = fmax(area, (i - k + 1) * width);
}
res = fmax(res, area);
}
}
return res;
}
- Python language version
class Solution:
def maximalRectangle(self, matrix: List[List[str]]) -> int:
m = len(matrix)
n = len(matrix[0])
left = [[0 for _ in range(n)] for _ in range (m)]
for i in range(0, m):
for j in range(0, n):
if matrix[i][j] == '1':
left[i][j] = (0 if j == 0 else left[i][j - 1]) + 1
res = 0
for i in range(0, m):
for j in range(0, n):
if matrix[i][j] == '0':
continue
width = left[i][j]
area = width
for k in range(i - 1, -1, -1):
width = min(width, left[k][j])
area = max(area, (i - k + 1) * width)
res = max(res, area)
return res
- C++ language version
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
int m = matrix.size();
int n = matrix[0].size();
vector<vector<int>> left(m, vector<int>(n, 0));
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j++){
if(matrix[i][j] == '1'){
left[i][j] = (j == 0 ? 0 : left[i][j - 1]) + 1;
}
}
}
int res = 0;
for(int i = 0;i < m;i++){
for(int j = 0;j < n;j++){
if(matrix[i][j] == '0'){
continue;
}
int width = left[i][j];
int area = width;
for(int k = i - 1;k >= 0;k--){
width = fmin(width, left[k][j]);
area = fmax(area, (i - k + 1) * width);
}
res = fmax(res, area);
}
}
return res;
}
};
Ten【Submission Results】
-
Java language version
-
C language version
-
Python language version
-
C++ language version