15% off all plans & roadmaps Use code
See plansLeetCode 85 · Solution & editorial
Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.
Maximal Rectangle is a Stack Patterns problem, in the Largest Rectangle in Histogram subpattern. The pattern covers parentheses matching, monotonic stack, histogram, and expression evaluation
Learn the stack-based approach to find largest rectangle in histogram and maximal rectangle in matrix.
The reference solution runs in O(m * n) time and O(n) space.
Maximal Rectangle has been reported in interviews at Google, Amazon, Adobe, Bloomberg, Apple.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Maximal Rectangle is a Stack Patterns problem, in the Largest Rectangle in Histogram subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.