Skip to content

Commit 3280690

Browse files
chore: add LeetCode daily solution
1 parent 1ace8bf commit 3280690

5 files changed

Lines changed: 149 additions & 0 deletions

File tree

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
class Solution {
2+
public int maximalRectangle(char[][] matrix) {
3+
if (matrix.length == 0) return 0;
4+
int maxArea = 0;
5+
int[] heights = new int[matrix[0].length];
6+
7+
for (int i = 0; i < matrix.length; i++) {
8+
for (int j = 0; j < matrix[0].length; j++) {
9+
if (matrix[i][j] == '1') {
10+
heights[j] += 1;
11+
} else {
12+
heights[j] = 0;
13+
}
14+
}
15+
maxArea = Math.max(maxArea, largestRectangleArea(heights));
16+
}
17+
18+
return maxArea;
19+
}
20+
21+
private int largestRectangleArea(int[] heights) {
22+
int maxArea = 0;
23+
int[] left = new int[heights.length];
24+
int[] right = new int[heights.length];
25+
Stack<Integer> stack = new Stack<>();
26+
27+
for (int i = 0; i < heights.length; i++) {
28+
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
29+
stack.pop();
30+
}
31+
left[i] = stack.isEmpty() ? -1 : stack.peek();
32+
stack.push(i);
33+
}
34+
35+
stack.clear();
36+
37+
for (int i = heights.length - 1; i >= 0; i--) {
38+
while (!stack.isEmpty() && heights[stack.peek()] >= heights[i]) {
39+
stack.pop();
40+
}
41+
right[i] = stack.isEmpty() ? heights.length : stack.peek();
42+
stack.push(i);
43+
}
44+
45+
for (int i = 0; i < heights.length; i++) {
46+
maxArea = Math.max(maxArea, (right[i] - left[i] - 1) * heights[i]);
47+
}
48+
49+
return maxArea;
50+
}
51+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
var maximalRectangle = function(matrix) {
2+
if (matrix.length === 0) return 0;
3+
4+
const rows = matrix.length;
5+
const cols = matrix[0].length;
6+
const heights = Array(cols).fill(0);
7+
let maxArea = 0;
8+
9+
for (let i = 0; i < rows; i++) {
10+
for (let j = 0; j < cols; j++) {
11+
heights[j] = matrix[i][j] === '1' ? heights[j] + 1 : 0;
12+
}
13+
maxArea = Math.max(maxArea, largestRectangleArea(heights));
14+
}
15+
16+
return maxArea;
17+
};
18+
19+
function largestRectangleArea(heights) {
20+
const stack = [];
21+
let maxArea = 0;
22+
heights.push(0); // Add a sentinel value
23+
24+
for (let i = 0; i < heights.length; i++) {
25+
while (stack.length && heights[stack[stack.length - 1]] > heights[i]) {
26+
const h = heights[stack.pop()];
27+
const w = stack.length ? i - stack[stack.length - 1] - 1 : i;
28+
maxArea = Math.max(maxArea, h * w);
29+
}
30+
stack.push(i);
31+
}
32+
33+
return maxArea;
34+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution:
2+
def maximalRectangle(self, matrix: List[List[str]]) -> int:
3+
if not matrix or not matrix[0]:
4+
return 0
5+
6+
max_area = 0
7+
heights = [0] * len(matrix[0])
8+
9+
for row in matrix:
10+
for i in range(len(row)):
11+
heights[i] = heights[i] + 1 if row[i] == '1' else 0
12+
13+
max_area = max(max_area, self.largestRectangleArea(heights))
14+
15+
return max_area
16+
17+
def largestRectangleArea(self, heights: List[int]) -> int:
18+
heights.append(0)
19+
stack = []
20+
max_area = 0
21+
22+
for i in range(len(heights)):
23+
while stack and heights[stack[-1]] > heights[i]:
24+
h = heights[stack.pop()]
25+
w = i if not stack else i - stack[-1] - 1
26+
max_area = max(max_area, h * w)
27+
stack.append(i)
28+
29+
return max_area

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
330330
- 2026-01-06 — [Maximum Level Sum of a Binary Tree](https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/) (Medium) → `Medium/2026-01-06-1161-Maximum-Level-Sum-of-a-Binary-Tree`
331331
- 2026-01-07 — [Maximum Product of Splitted Binary Tree](https://leetcode.com/problems/maximum-product-of-splitted-binary-tree/) (Medium) → `Medium/2026-01-07-1339-Maximum-Product-of-Splitted-Binary-Tree`
332332
- 2026-01-08 — [Max Dot Product of Two Subsequences](https://leetcode.com/problems/max-dot-product-of-two-subsequences/) (Hard) → `Hard/2026-01-08-1458-Max-Dot-Product-of-Two-Subsequences`
333+
- 2026-01-11 — [Maximal Rectangle](https://leetcode.com/problems/maximal-rectangle/) (Hard) → `Hard/2026-01-11-85-Maximal-Rectangle`

0 commit comments

Comments
 (0)