Skip to content
Decoder

Maximal Rectangle

Hard

Problem

Given a grid matrix filled with the characters "0" and "1", return the area of the largest rectangle containing only `"1"`s. The rectangle's sides follow the grid lines.

Example 1

matrix = [

Example 2

  ["1","0","1","0","0"],

Example 3

  ["1","0","1","1","1"],

Example 4

  ["1","1","1","1","1"],

Example 5

  ["1","0","0","1","0"],

Example 6

]

Example 7

→  6   (rows 1 and 2, columns 2 to 4)

Example 8

Input
matrix = [["0"]]
Output
0

Example 9

Input
matrix = [["1"]]
Output
1

Constraints

  • rows == matrix.length, cols == matrix[i].length
  • 1 <= rows, cols <= 200
  • matrix[i][j] is "0" or "1".

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.