# Question

Formatted question description: https://leetcode.ca/all/85.html

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.

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

@tag-stack


# Algorithm

This question is an extension of the previous 84. Largest Rectangle in Histogram. The two-dimensional matrix of this question can be seen as a histogram at each level upwards.

For each histogram to call the method in 84. Largest Rectangle in Histogram to get the largest rectangle area.

Then the only thing to do in this question is to treat each layer as the bottom layer of the histogram, and construct the entire histogram upwards. Since the question limits the characters of the input matrix to only ‘0’ and ‘1’, it is processed It is also relatively simple. The method is, for each point, if it is ‘0’, assign 0, if it is ‘1’, assign the previous height value plus 1.

Java