Maximum Rectangle
Thoughts
Code
class Solution {
public int maximalRectangle(char[][] matrix) {
int m = matrix.length, n = m == 0 ? 0 : matrix[0].length;
if (m == 0 || n == 0) {
return 0;
}
int[] height = new int[n];
int[] left = new int[n];
int[] right = new int[n];
Arrays.fill(right, n);
int res = 0;
for (int i = 0; i < m; i++) {
int curLeft = 0, curRight = n;
for (int j = 0; j < n; j++) {
if (matrix[i][j] == '1') {
height[j] += 1;
} else {
height[j] = 0;
}
}
for (int j = 0; j < n; j++) {
if (matrix[i][j] == '1') {
left[j] = Math.max(left[j], curLeft);
} else {
curLeft = j + 1;
left[j] = 0;
}
}
for (int j = n - 1; j >= 0; j--) {
if (matrix[i][j] == '1') {
right[j] = Math.min(right[j], curRight);
} else {
curRight = j;
right[j] = n;
}
}
for (int j = 0; j < n; j++) {
res = Math.max(res, (right[j] - left[j]) * height[j]);
}
}
return res;
}
}Previous1334. Find the City With the Smallest Number of Neighbors at a Threshold DistanceNext1513. Number of Substrings With Only 1s
Last updated