Advertisement

Maximal Rectangle - LeetCode 85 Solution

Maximal Rectangle - Complete Solution Guide

Maximal Rectangle is LeetCode problem 85, a Hard level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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 matr

Detailed Explanation

The problem asks us to find the largest rectangular area containing only '1's within a given binary matrix (a matrix filled with '0's and '1's). The input is a 2D array (matrix) of characters, and the output is an integer representing the area of the largest rectangle found. The matrix can have different dimensions (rows and columns), and the constraints specify the size limits for rows and columns and that matrix elements are either '0' or '1'.

Solution Approach

The solution iterates through each row of the matrix. For each row, it calculates the height of consecutive '1's in each column, essentially forming a histogram. Then, it uses a stack-based approach to find the largest rectangular area in that histogram. The maximum area found across all rows is the final result. The stack stores the indices of bars in increasing order of height. When a smaller height is encountered, the stack is popped to calculate areas until the stack's top has a height less than or equal to the current height.

Step-by-Step Algorithm

  1. Step 1: Initialize an array `heights` of size `cols + 1` with all values set to 0. This array stores the height of the histogram for each column.
  2. Step 2: Iterate through each row of the matrix.
  3. Step 3: For each row, update the `heights` array. If `matrix[r][c]` is '1', increment `heights[c]`; otherwise, set `heights[c]` to 0.
  4. Step 4: Use a stack to find the largest rectangle in the current histogram (represented by `heights`).
  5. Step 5: Initialize the stack with -1. This acts as a sentinel value to simplify the calculation of the width when the stack becomes empty.
  6. Step 6: Iterate through the `heights` array using index `i`.
  7. Step 7: While the stack is not empty and `heights[stack.peek()]` is greater than `heights[i]`, pop elements from the stack.
  8. Step 8: Calculate the area of the rectangle formed by the popped height. The height is `heights[stack.pop()]` and the width is `i - stack.peek() - 1`.
  9. Step 9: Update `max_area` with the maximum area found so far.
  10. Step 10: Push the current index `i` onto the stack.
  11. Step 11: After iterating through all rows, return the final `max_area`.

Key Insights

  • Insight 1: The problem can be solved by iterating through each row of the matrix and treating the 'height' of consecutive '1's in each column as a histogram. This transforms the 2D problem into a series of 1D largest rectangle in histogram problems.
  • Insight 2: The largest rectangle in a histogram can be efficiently found using a stack to keep track of increasing heights. The stack helps determine the left and right boundaries of a potential rectangle for each height.
  • Insight 3: The matrix contains characters '0' and '1', not integers. We need to ensure we properly handle this during processing.

Complexity Analysis

Time Complexity: O(m*n)

Space Complexity: O(n)

Topics

This problem involves: Array, Dynamic Programming, Stack, Matrix, Monotonic Stack.

Companies

Asked at: Adobe, Amazon, Apple, Bloomberg, DE Shaw, Flipkart, Google, Huawei, Intuit, Meta, Microsoft, Oracle, TikTok, Uber, Yahoo.