101 Logo
onenoughtone

Problem Statement

Maximal Rectangle

You are an urban planner working on a city development project. You have a grid-based map where each cell represents a plot of land. A value of '1' indicates a plot is available for development, while '0' indicates a plot that cannot be used.

Your task is to find the largest rectangular area of available plots (containing only 1's) that can be developed as a single project. This will maximize the land usage efficiency for the city.

Write a function that takes a binary matrix (grid of 0's and 1's) and returns the area of the largest rectangle containing only 1's.

Examples

Example 1:

Input: [ ["1","0","1","0","0"], ["1","0","1","1","1"], ["1","1","1","1","1"], ["1","0","0","1","0"] ]
Output: 6
Explanation: The maximal rectangle is shown in the above grid. The area of this rectangle is 6 (2×3).

Example 2:

Input: [ ["0","1"], ["1","0"] ]
Output: 1
Explanation: Each '1' forms a rectangle of area 1, and there are no larger rectangles.

Example 3:

Input: [["0"]]
Output: 0
Explanation: There are no '1's in the grid, so the maximal rectangle has area 0.

Constraints

  • The number of rows m is between 0 and 200.
  • The number of columns n is between 0 and 200.
  • Each cell in the matrix contains either '0' or '1'.
  • The matrix is represented as a 2D array of characters, not integers.

Problem Breakdown

To solve this problem, we need to:

  1. This problem can be reduced to finding the largest rectangle in a histogram for each row.
  2. Dynamic programming can be used to efficiently calculate the heights for each position.
  3. A stack-based approach can efficiently find the largest rectangle in a histogram.
  4. The problem requires considering both horizontal and vertical continuity of 1's.
ProblemSolutionCode
101 Logo
onenoughtone