|
| 1 | +# [Problem 85: Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/description/?envType=daily-question) |
| 2 | + |
| 3 | +## Initial thoughts (stream-of-consciousness) |
| 4 | +I need the largest-area rectangle containing only '1's in a binary matrix. A brute-force approach would enumerate all possible rectangles and check if they're all '1's, but that's O((mn)^2) rectangles times O(mn) check — far too slow for m,n up to 200. |
| 5 | + |
| 6 | +I recall a common trick: treat each row as the base of a histogram where the height at each column is the number of consecutive '1's up to that row. Then the problem reduces, for each row, to "largest rectangle in histogram" which can be solved in O(n) using a monotonic stack. So overall O(m * n) time. That seems promising. |
| 7 | + |
| 8 | +I'll update heights row-by-row and compute largest-rectangle-in-histogram each time. Need to handle edge cases: empty matrix; rows or columns equal to 1; all zeros. |
| 9 | + |
| 10 | +## Refining the problem, round 2 thoughts |
| 11 | +- For each row i, compute heights[j] = heights[j] + 1 if matrix[i][j] == '1' else 0. |
| 12 | +- Compute largest rectangle area from heights with the standard mono-decreasing stack using a sentinel (append a 0). |
| 13 | +- Complexity: for m rows and n columns, updating heights is O(n) per row, histogram algorithm is O(n) per row, total O(m*n). Space is O(n) for heights and stack. |
| 14 | +- Edge cases: if matrix is empty, return 0. If a row has all zeros, heights will reset to zero and histogram will yield 0 correctly. |
| 15 | +- Implementation detail: do not mutate heights length permanently; use a temporary heights_with_sentinel = heights + [0] when scanning with stack. |
| 16 | + |
| 17 | +## Attempted solution(s) |
| 18 | +```python |
| 19 | +from typing import List |
| 20 | + |
| 21 | +class Solution: |
| 22 | + def maximalRectangle(self, matrix: List[List[str]]) -> int: |
| 23 | + if not matrix or not matrix[0]: |
| 24 | + return 0 |
| 25 | + |
| 26 | + rows, cols = len(matrix), len(matrix[0]) |
| 27 | + heights = [0] * cols |
| 28 | + max_area = 0 |
| 29 | + |
| 30 | + for r in range(rows): |
| 31 | + # update histogram heights for this row |
| 32 | + for c in range(cols): |
| 33 | + if matrix[r][c] == '1': |
| 34 | + heights[c] += 1 |
| 35 | + else: |
| 36 | + heights[c] = 0 |
| 37 | + |
| 38 | + # compute largest rectangle in histogram heights |
| 39 | + # use a monotonic increasing stack of indices |
| 40 | + stack = [] |
| 41 | + extended = heights + [0] # sentinel to flush stack at the end |
| 42 | + for i, h in enumerate(extended): |
| 43 | + while stack and extended[stack[-1]] > h: |
| 44 | + height = extended[stack.pop()] |
| 45 | + width = i if not stack else i - stack[-1] - 1 |
| 46 | + area = height * width |
| 47 | + if area > max_area: |
| 48 | + max_area = area |
| 49 | + stack.append(i) |
| 50 | + |
| 51 | + return max_area |
| 52 | +``` |
| 53 | +- Notes: |
| 54 | + - Approach: treat each row as the base of a histogram of consecutive '1's above that row; for each histogram compute largest rectangle using a monotonic stack. |
| 55 | + - Time complexity: O(rows * cols) — each cell is processed a constant number of times. |
| 56 | + - Space complexity: O(cols) for the heights array and stack. |
| 57 | + - Implementation details: append a 0 sentinel to flush out remaining heights; widths are computed from popped index and current index with stack top as previous smaller index. Handles empty matrix and all-zero rows correctly. |
0 commit comments