LeetCode 0085. Maximal Rectangle Solution in Java, Python, C++, JavaScript, Go & Rust | Explanation + Code

CoderIndeed
0
0085. Maximal Rectangle

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 <= rows, cols <= 200
  • matrix[i][j] is '0' or '1'.

Solutions

Solution 1: Monotonic Stack

We can treat each row as the base of a histogram and calculate the maximum area of the histogram for each row.

Specifically, we maintain an array heights with the same length as the number of columns in the matrix, where heights[j] represents the height of the column at the j-th position with the current row as the base. For each row, we iterate through each column:

  • If the current element is '1', increment heights[j] by 1.
  • If the current element is '0', set heights[j] to 0.

Then, we use the monotonic stack algorithm to calculate the maximum rectangle area of the current histogram and update the answer.

The specific steps of the monotonic stack are as follows:

  1. Initialize an empty stack stk to store the indices of the columns.
  2. Initialize two arrays left and right, representing the index of the first column to the left and right of each column that is shorter than the current column.
  3. Iterate through the heights array heights, first calculating the index of the first column to the left of each column that is shorter than the current column, and store it in left.
  4. Then iterate through the heights array heights in reverse order, calculating the index of the first column to the right of each column that is shorter than the current column, and store it in right.
  5. Finally, calculate the maximum rectangle area for each column and update the answer.

The time complexity is O(m × n), where m is the number of rows in matrix and n is the number of columns in matrix.

PythonJavaC++GoTypeScriptRustC#
class Solution: def maximalRectangle(self, matrix: List[List[str]]) -> int: heights = [0] * len(matrix[0]) ans = 0 for row in matrix: for j, v in enumerate(row): if v == "1": heights[j] += 1 else: heights[j] = 0 ans = max(ans, self.largestRectangleArea(heights)) return ans def largestRectangleArea(self, heights: List[int]) -> int: n = len(heights) stk = [] left = [-1] * n right = [n] * n for i, h in enumerate(heights): while stk and heights[stk[-1]] >= h: stk.pop() if stk: left[i] = stk[-1] stk.append(i) stk = [] for i in range(n - 1, -1, -1): h = heights[i] while stk and heights[stk[-1]] >= h: stk.pop() if stk: right[i] = stk[-1] stk.append(i) return max(h * (right[i] - left[i] - 1) for i, h in enumerate(heights))(code-box)

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !