#85

Maximal Rectangle

Hard Histogram per row

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

In a binary matrix of '0' and '1', return the area of the largest rectangle containing only '1's.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the largest all-'1' rectangle in a grid.

Why this works: treat each row as the floor of a histogram of consecutive 1s, then run "largest rectangle in histogram".

Example: rows build heights like [3, 1, 3, 2, 2]

histogram answer for that row = 6best over all rows = answer

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Not resetting the height to 0 when a '0' appears, which lets rectangles pass through zeros.

Solve on LeetCode ↗
Complexity
  • Time: O(m·n).
  • Space: O(n).
Topic: Stack & Queue
🎬 Episodes 46–52: video coming soon