class Solution:
def maximalRectangle(self, matrix):
if not matrix or not matrix[0]:
return 0
max_area = 0
n = len(matrix[0])
heights = [0] * (n + 1) # Add a dummy zero at the end
for row in matrix:
for i in range(n):
if row[i] == '1':
heights[i] += 1
else:
heights[i] = 0
stack = [-1]
for i in range(n + 1):
while heights[i] < heights[stack[-1]]:
h = heights[stack.pop()]
w = i - stack[-1] - 1
max_area = max(max_area, h * w)
stack.append(i)
return max_area
class Solution {
public:
int maximalRectangle(vector<vector<char>>& matrix) {
if (matrix.empty() || matrix[0].empty()) return 0;
int maxArea = 0, n = matrix[0].size();
vector<int> heights(n + 1, 0); // Add a dummy zero at the end
for (const auto& row : matrix) {
for (int i = 0; i < n; ++i) {
heights[i] = (row[i] == '1') ? heights[i] + 1 : 0;
}
stack<int> stk;
stk.push(-1);
for (int i = 0; i <= n; ++i) {
while (stk.top() != -1 && heights[i] < heights[stk.top()]) {
int h = heights[stk.top()]; stk.pop();
int w = i - stk.top() - 1;
maxArea = max(maxArea, h * w);
}
stk.push(i);
}
}
return maxArea;
}
};
class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix.length == 0 || matrix[0].length == 0) return 0;
int maxArea = 0;
int n = matrix[0].length;
int[] heights = new int[n + 1]; // Add a dummy zero at the end
for (char[] row : matrix) {
for (int i = 0; i < n; i++) {
if (row[i] == '1') heights[i]++;
else heights[i] = 0;
}
Stack<Integer> stack = new Stack<>();
stack.push(-1);
for (int i = 0; i <= n; i++) {
while (stack.peek() != -1 && heights[i] < heights[stack.peek()]) {
int h = heights[stack.pop()];
int w = i - stack.peek() - 1;
maxArea = Math.max(maxArea, h * w);
}
stack.push(i);
}
}
return maxArea;
}
}
var maximalRectangle = function(matrix) {
if (!matrix.length || !matrix[0].length) return 0;
let maxArea = 0, n = matrix[0].length;
let heights = new Array(n + 1).fill(0); // Add a dummy zero at the end
for (let row of matrix) {
for (let i = 0; i < n; i++) {
heights[i] = row[i] === '1' ? heights[i] + 1 : 0;
}
let stack = [-1];
for (let i = 0; i <= n; i++) {
while (heights[i] < heights[stack[stack.length - 1]]) {
let h = heights[stack.pop()];
let w = i - stack[stack.length - 1] - 1;
maxArea = Math.max(maxArea, h * w);
}
stack.push(i);
}
}
return maxArea;
};
matrix
filled with '0's and '1's, find the largest rectangle containing only '1's and return its area. Each cell in matrix
can only be used once, and rectangles must be made up of consecutive rows and columns. The rectangle must be entirely filled with '1's, and you must return the area (the number of cells) of the largest such rectangle. There is only one valid area for the largest rectangle.
heights
where heights[i]
is the number of consecutive '1's in column i
up to the current row.heights
by incrementing for '1's and resetting to zero for '0's.heights
array (i.e., for each row), apply the "Largest Rectangle in Histogram" algorithm:
matrix
is:
[ ['1','0','1','0','0'], ['1','0','1','1','1'], ['1','1','1','1','1'], ['1','0','0','1','0'] ]Let's walk through each row:
heights = [1,0,1,0,0]
→ Largest rectangle is 1.heights = [2,0,2,1,1]
→ Largest rectangle is 3 (at columns 2,3,4).heights = [3,1,3,2,2]
→ Largest rectangle is 6 (columns 0,2,3,4, height 2).heights = [4,0,0,3,0]
→ Largest rectangle is 4 (column 0, height 4).