Skip to content

85. Maximal Rectangle

Difficulty Topics

Description

Given a rows x cols binary matrix filled with 0's and 1's, find the largest rectangle containing only 1's and return its area.

 

Example 1:

Input: matrix = [["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 picture.

Example 2:

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

Example 3:

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

 

Constraints:

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

Solution

maximal-rectangle.py
class Solution:
    def maximalRectangle(self, matrix: List[List[str]]) -> int:
        if not matrix: return 0

        rows, cols = len(matrix), len(matrix[0])
        left = [0] * cols
        right = [cols] * cols
        heights = [0] * cols
        res = 0

        for i in range(rows):
            currLeft, currRight = 0, cols

            for j in range(cols):
                if matrix[i][j] == "1":
                    heights[j] += 1
                else:
                    heights[j] = 0

            for j in range(cols):
                if matrix[i][j] == "1":
                    left[j] = max(left[j], currLeft)
                else:
                    left[j] = 0
                    currLeft = j + 1

            for j in range(cols - 1, -1, -1):
                if matrix[i][j] == "1":
                    right[j] = min(right[j], currRight)
                else:
                    right[j] = cols
                    currRight = j

            for j in range(cols):
                res = max(res, (right[j] - left[j]) * heights[j])

        return res