Maximal Rectangle
Problem Statement
You are given a grid matrix of rows × cols cells. Each cell holds the character '0' or '1'.
Find the biggest rectangle (with sides along the grid lines) that contains only '1' cells, and return its area.
A 2D grid matrix of characters, each '0' or '1'.
One integer: the area of the largest all-'1' rectangle.
Rows 1–2, columns 2–4 are all '1': a 2 × 3 rectangle with area 6.
- • 1 ≤ rows, cols ≤ 200
- • matrix[i][j] is '0' or '1'
Pattern Recognition Clues
Recognising these key signals in the problem description immediately identifies the pattern:
Approach Complexity Ladder
For each cell, grow the rectangle upwards keeping the narrowest row width
Build column heights row by row and run Largest Rectangle in Histogram on each row
First count, for each cell, how many '1's in a row end there going left (width[r][c]). Then treat each cell as the bottom-right corner of a rectangle and walk upwards. The rectangle can only be as wide as its narrowest row, so keep a running minimum of width and check minWidth × height at each step.
Recognition rule for this sub-pattern:
Unlock Complete Visual Walkthrough & Optimal C++
The interactive animated dry-run player, optimized C++ implementation, state invariant derivations, and edge-case breakdown for this question are part of Brute2Best Pro. The anchor question of every sub-pattern is completely free.
First anchor question per pattern, Part 1 LLD & Module 1 C++ basics are 100% Free forever.
How this concept connects to System Design & C++ Systems
Top engineering interviews test whether you understand how algorithms like Monotonic Stack translate into production architectures and low-level memory mechanics.