Imagine you're a city planner looking at a blueprint from above, where 1's represent buildable land and 0's represent water or restricted areas. Your task is to find the largest rectangular plot that can be built on, containing only buildable land (1's).

Given a rows ร— cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.

Example: In a matrix like [[1,0,1,0,0],[1,0,1,1,1],[1,1,1,1,1],[1,0,0,1,0]], the largest rectangle of 1's has area 6 (a 2ร—3 rectangle).

Input & Output

example_1.py โ€” Standard Case
$ Input: matrix = [["1","0","1","0","0"],["1","0","1","1","1"],["1","1","1","1","1"],["1","0","0","1","0"]]
โ€บ Output: 6
๐Ÿ’ก Note: The maximal rectangle is formed by the 2ร—3 rectangle in the bottom-right area, containing only 1's with area 6
example_2.py โ€” Single Row
$ Input: matrix = [["1"]]
โ€บ Output: 1
๐Ÿ’ก Note: Single cell containing '1' forms a rectangle of area 1
example_3.py โ€” All Zeros
$ Input: matrix = [["0","0"],["0","0"]]
โ€บ Output: 0
๐Ÿ’ก Note: No rectangle can be formed with only 1's since all cells contain 0's

Visualization

Tap to expand
๐Ÿ—๏ธ City Planning: Finding the Perfect Building PlotBlueprint View (1 = Buildable, 0 = Restricted)Perfect Plot: 3ร—2 = 6 unitsBuilding Height AnalysisRow 0 heights: [1, 0, 1, 1, 1]Row 1 heights: [2, 0, 2, 2, 2]Row 2 heights: [3, 1, 3, 3, 3]Smart Analysis (Monotonic Stack):โ€ข Process each height in histogramโ€ข Use stack to efficiently find rectangles๐ŸŽฏ The City Planner's StrategyStep 1: Survey Each Rowโ€ข Look at each row as a foundation levelโ€ข Calculate how many floors can be built at each columnStep 2: Smart Rectangle Detectionโ€ข Use monotonic stack (like smart measuring tools)โ€ข Efficiently find largest rectangle in each row's histogramResult: Perfect rectangular plot for the shopping mall!โœจ Time: O(mn) | Space: O(n) - Optimal city planning solution!
Understanding the Visualization
1
Survey the Land
Look at the blueprint where 1's are buildable land and 0's are restricted areas
2
Build Height Profile
For each row, measure how tall we could build at each column (consecutive 1's above)
3
Find Best Rectangle
Use smart measuring tools (monotonic stack) to efficiently find the largest rectangular plot
4
Compare All Options
Repeat for each row and keep track of the best rectangular plot found
Key Takeaway
๐ŸŽฏ Key Insight: Transform the 2D rectangle problem into multiple 1D histogram problems, then use a monotonic stack to efficiently solve each histogram in linear time.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(mn)

O(mn) to build heights + O(mn) for histogram processing (each element pushed/popped once)

n
2n
โœ“ Linear Growth
Space Complexity
O(n)

O(n) for heights array + O(n) for stack in worst case

n
2n
โšก Linearithmic Space

Constraints

  • rows == matrix.length
  • cols == matrix[i].length
  • 1 โ‰ค rows, cols โ‰ค 200
  • matrix[i][j] is '0' or '1'
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
48.0K Views
High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen