/**
* Problem: LeetCode 85 - Maximal Rectangle
* Difficulty: Hard
* Language: Java
* Author: CodeByTushu
*/
import java.util.ArrayDeque;
import java.util.Deque;
class Solution {
public int maximalRectangle(char[][] matrix) {
if (matrix == null || matrix.length == 0 || matrix[0].length == 0) {
return 0;
}
int rows = matrix.length;
int cols = matrix[0].length;
int[] heights = new int[cols];
int maxArea = 0;
for (int i = 0; i < rows; i++) {
// Update running heights of contiguous '1's for the current row
for (int j = 0; j < cols; j++) {
if (matrix[i][j] == '1') {
heights[j]++;
} else {
heights[j] = 0;
}
}
// Compute the largest rectangle in histogram for the current row
maxArea = Math.max(maxArea, largestRectangleArea(heights));
}
return maxArea;
}
private int largestRectangleArea(int[] heights) {
int n = heights.length;
Deque<Integer> stack = new ArrayDeque<>();
int maxArea = 0;
for (int i = 0; i <= n; i++) {
int currentHeight = (i == n) ? 0 : heights[i];
while (!stack.isEmpty() && currentHeight < heights[stack.peek()]) {
int height = heights[stack.pop()];
int width = stack.isEmpty() ? i : i - stack.peek() - 1;
maxArea = Math.max(maxArea, height * width);
}
stack.push(i);
}
return maxArea;
}
}
"""
Problem: LeetCode 85 - Maximal Rectangle
Difficulty: Hard
Language: Python
Author: CodeByTushu
"""
from typing import List
class Solution:
def maximalRectangle(self, matrix: List[List[str]]) -> int:
if not matrix or not matrix[0]:
return 0
cols = len(matrix[0])
heights = [0] * cols
max_area = 0
for row in matrix:
# Update running heights of contiguous '1's for the current row
for j in range(cols):
if row[j] == '1':
heights[j] += 1
else:
heights[j] = 0
# Compute largest rectangle in histogram for the current row
max_area = max(max_area, self._largest_rectangle_area(heights))
return max_area
def _largest_rectangle_area(self, heights: List[int]) -> int:
stack = []
max_area = 0
extended_heights = heights + [0]
for i, h in enumerate(extended_heights):
while stack and extended_heights[stack[-1]] > h:
height = extended_heights[stack.pop()]
width = i if not stack else i - stack[-1] - 1
max_area = max(max_area, height * width)
stack.append(i)
return max_area
/**
* Problem: LeetCode 85 - Maximal Rectangle
* Difficulty: Hard
* Language: C++
* Author: CodeByTushu
*/
#include <vector>
#include <stack>
#include <algorithm>
class Solution {
public:
int maximalRectangle(std::vector<std::vector<char>>& matrix) {
if (matrix.empty() || matrix[0].empty()) {
return 0;
}
int rows = matrix.size();
int cols = matrix[0].size();
std::vector<int> heights(cols, 0);
int maxArea = 0;
for (int i = 0; i < rows; ++i) {
// Update running heights of contiguous '1's for the current row
for (int j = 0; j < cols; ++j) {
if (matrix[i][j] == '1') {
heights[j]++;
} else {
heights[j] = 0;
}
}
// Compute largest rectangle in histogram for the current row
maxArea = std::max(maxArea, largestRectangleArea(heights));
}
return maxArea;
}
private:
int largestRectangleArea(const std::vector<int>& heights) {
int n = heights.size();
std::stack<int> st;
int maxArea = 0;
for (int i = 0; i <= n; ++i) {
int currentHeight = (i == n) ? 0 : heights[i];
while (!st.empty() && currentHeight < heights[st.top()]) {
int height = heights[st.top()];
st.pop();
int width = st.empty() ? i : i - st.top() - 1;
maxArea = std::max(maxArea, height * width);
}
st.push(i);
}
return maxArea;
}
};
/**
* Problem: LeetCode 85 - Maximal Rectangle
* Difficulty: Hard
* Language: JavaScript
* Author: CodeByTushu
*/
/**
* @param {character[][]} matrix
* @return {number}
*/
var maximalRectangle = function(matrix) {
if (!matrix || matrix.length === 0 || matrix[0].length === 0) {
return 0;
}
const rows = matrix.length;
const cols = matrix[0].length;
const heights = new Array(cols).fill(0);
let maxArea = 0;
for (let i = 0; i < rows; i++) {
// Update running heights of contiguous '1's for the current row
for (let j = 0; j < cols; j++) {
if (matrix[i][j] === '1') {
heights[j]++;
} else {
heights[j] = 0;
}
}
// Compute largest rectangle in histogram for the current row
maxArea = Math.max(maxArea, largestRectangleArea(heights));
}
return maxArea;
};
/**
* Helper to compute Largest Rectangle in Histogram using Monotonic Stack
* @param {number[]} heights
* @return {number}
*/
function largestRectangleArea(heights) {
const stack = [];
let maxArea = 0;
const n = heights.length;
for (let i = 0; i <= n; i++) {
const currentHeight = (i === n) ? 0 : heights[i];
while (stack.length > 0 && currentHeight < heights[stack[stack.length - 1]]) {
const height = heights[stack.pop()];
const width = stack.length === 0 ? i : i - stack[stack.length - 1] - 1;
maxArea = Math.max(maxArea, height * width);
}
stack.push(i);
}
return maxArea;
}