Problem
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:
1 | |
Explanation
-
We can use 84. Largest Rectangle in Histogram’s solution to solve this problem.
-
In the input matrix array, we can think of each row is the
heightsarray. For example, the first row is the heights array[1, 0, 1, 0, 0], the second row is the heights array[2, 0, 2, 1, 1], the third row is the array[3, 1, 3, 2, 2], the fourth row is[4, 0, 0, 3, 0]. Then we compute each row’s maximum area by using the 84. Largest Rectangle in Histogram’s function.
Solution
1 | |