Skip to main content

Maximum Rectangle in Binary Tile Grid

hard
ArrayDynamic ProgrammingMatrixMonotonic StackDp 1d
Asked atAmazonGoogleMicrosoftMeta

Problem Description

You are given a 2D grid of `'0'` and `'1'` characters representing a binary map. Find the area of the largest rectangle containing only `'1'` tiles.

**Example 1:**
```
Input: grid = [
['1','0','1','0'],
['1','0','1','1'],
['1','1','1','1'],
['0','0','1','1']
]
Output: 6
Explanation: The largest all-1 rectangle has area 6 (rows 1-3, cols 2-3 plus row 2 col 1).
```

**Example 2:**
```
Input: grid = [['0']]
Output: 0
```

Constraints

  • 1 <= grid.length, grid[0].length <= 200
  • grid[i][j] is '0' or '1'

Follow-up

What if the grid can also contain `'2'` tiles that act as obstacles (treated like `'0'`) but you must count all distinct all-`'1'` maximal rectangles?

Hints

Try the problem first. If you get stuck, you can reveal hints one at a time.

Solution

Leaderboard

No entries yet for python.

Be the first — submit an accepted solution.