|
| 1 | +# Maximal Rectangle (Hard) |
| 2 | + |
| 3 | +**Problem ID:** 85 |
| 4 | +**Date:** 2026-01-11 |
| 5 | +**Link:** https://leetcode.com/problems/maximal-rectangle/ |
| 6 | + |
| 7 | +## Approach |
| 8 | + |
| 9 | +To solve the "Maximal Rectangle" problem, we can leverage a dynamic programming approach combined with the concept of the largest rectangle in a histogram. The main idea is to treat each row of the binary matrix as the base of a histogram where the height of each column represents the number of consecutive '1's up to that row. |
| 10 | + |
| 11 | +### Approach: |
| 12 | + |
| 13 | +1. **Transform the Matrix**: |
| 14 | + - Create an auxiliary array `heights` of size equal to the number of columns in the matrix. This array will store the heights of '1's for each column as we iterate through each row of the matrix. |
| 15 | + - For each cell in the matrix, if the cell contains '1', increment the corresponding height in `heights` by 1; if it contains '0', reset that height to 0. |
| 16 | + |
| 17 | +2. **Calculate Maximum Rectangle for Each Row**: |
| 18 | + - For each row in the matrix, after updating the `heights` array, treat `heights` as a histogram and compute the largest rectangle that can be formed using the heights. This can be efficiently done using a stack-based approach which allows us to find the largest rectangle area in O(cols) time. |
| 19 | + |
| 20 | +3. **Iterate Through Rows**: |
| 21 | + - Repeat the process for each row in the matrix, updating the `heights` and calculating the maximum rectangle area for each updated histogram. |
| 22 | + |
| 23 | +4. **Track the Maximum Area**: |
| 24 | + - Maintain a variable to track the maximum area encountered during these calculations. |
| 25 | + |
| 26 | +### Data Structures: |
| 27 | +- An array `heights` to store the heights of '1's in each column. |
| 28 | +- A stack to efficiently compute the largest rectangle area in the histogram. |
| 29 | + |
| 30 | +### Complexity: |
| 31 | +- **Time Complexity**: O(rows * cols), where `rows` is the number of rows and `cols` is the number of columns in the matrix. This is because we process each cell once and compute the histogram area in linear time for each row. |
| 32 | +- **Space Complexity**: O(cols) for the `heights` array and O(cols) for the stack, leading to an overall space complexity of O(cols). |
| 33 | + |
| 34 | +This approach effectively reduces the problem of finding the maximal rectangle in a binary matrix to a series of histogram problems, allowing us to solve it efficiently. |
0 commit comments