Problem 447
Maximal Rectangle
Hard · 30 pts · Matrix, Dynamic Programming, Stack, Monotonic Stack
Problem
In a grid of '0' and '1', find the largest rectangle that contains only '1's and print its area. Treat each row as the floor of a histogram: the height of a column is the number of consecutive '1's ending at that row. The largest rectangle in each histogram can then be found with a monotonic stack, giving an O(R · C) solution overall.
Input
First line: R and C. Next R lines: C characters, each '0' or '1'.
Output
Print the area of the largest all-ones rectangle.
Constraints
1 <= R, C <= 200
Example input
4 5 10100 10111 11111 10010
Example output
6
Explanation
In the grid 10100 / 10111 / 11111 / 10010, the rows 2 and 3 share '111' in the last three columns, forming a 2 x 3 rectangle of area 6. No larger all-ones rectangle exists.