Given a 2D binary matrix mat (values 0 or 1), find the area (number of cells) of the largest axis-aligned rectangle whose border cells are all 1. The interior of the rectangle may contain 0s.
classSolution:
deflargest1BorderedRectangle(self, mat: list[list[int]]) -> int:
n = len(mat)
m = len(mat[0])
ans =0for r1 in range(n):
for r2 in range(r1, n):
for c1 in range(m):
for c2 in range(c1, m):
ok =Truefor j in range(c1, c2+1):
if mat[r1][j] ==0or mat[r2][j] ==0:
ok =Falsebreakifnot ok:
continuefor i in range(r1, r2+1):
if mat[i][c1] ==0or mat[i][c2] ==0:
ok =Falsebreakif ok:
ans = max(ans, (r2 - r1 +1) * (c2 - c1 +1))
return ans
⏰ Time complexity: O(n^4) — four nested loops enumerating rows and columns and border checks in O(n) are subsumed by the enumeration worst-case; for an n x n matrix this is quartic.
Precompute at each cell hor[i][j] = number of consecutive 1s ending at (i,j) horizontally, and ver[i][j] = number of consecutive 1s ending at (i,j) vertically. For a candidate bottom-right corner (i,j), any rectangle of width w and height h ending at (i,j) is valid if hor[i][j] >= w, ver[i][j] >= h, ver[i][j-w+1] >= h and hor[i-h+1][j] >= w — each check is O(1).
For each cell (i,j) as bottom-right corner, iterate possible heights h = 1..ver[i][j] and widths w = 1..hor[i][j] (or iterate w/h in nested loops). For each pair check the four O(1) conditions above; update ans.
classSolution {
public:int largest1BorderedRectangle(std::vector<std::vector<int>>& mat) {
int n = mat.size();
int m = mat[0].size();
std::vector<std::vector<int>> hor(n, std::vector<int>(m));
std::vector<std::vector<int>> ver(n, std::vector<int>(m));
for (int i =0; i < n; ++i) {
for (int j =0; j < m; ++j) {
if (mat[i][j] ==1) {
hor[i][j] = (j ? hor[i][j-1] :0) +1;
ver[i][j] = (i ? ver[i-1][j] :0) +1;
}
}
}
int ans =0;
for (int i =0; i < n; ++i) {
for (int j =0; j < m; ++j) {
for (int h =1; h <= ver[i][j]; ++h) {
for (int w =1; w <= hor[i][j]; ++w) {
int r1 = i - h +1;
int c1 = j - w +1;
if (ver[i][c1] >= h && hor[r1][j] >= w) {
ans = std::max(ans, h * w);
}
}
}
}
}
return ans;
}
};
classSolution {
publicintlargest1BorderedRectangle(int[][] mat) {
int n = mat.length, m = mat[0].length;
int[][] hor =newint[n][m], ver =newint[n][m];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (mat[i][j]== 1) {
hor[i][j]= (j > 0 ? hor[i][j-1] : 0) + 1;
ver[i][j]= (i > 0 ? ver[i-1][j] : 0) + 1;
}
}
}
int ans = 0;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
for (int h = 1; h <= ver[i][j]; ++h) {
for (int w = 1; w <= hor[i][j]; ++w) {
int r1 = i - h + 1;
int c1 = j - w + 1;
if (ver[i][c1]>= h && hor[r1][j]>= w) ans = Math.max(ans, h * w);
}
}
}
}
return ans;
}
}
classSolution:
deflargest1BorderedRectangle(self, mat: list[list[int]]) -> int:
n = len(mat)
m = len(mat[0])
hor = [[0]*m for _ in range(n)]
ver = [[0]*m for _ in range(n)]
for i in range(n):
for j in range(m):
if mat[i][j] ==1:
hor[i][j] = (hor[i][j-1] if j>0else0) +1 ver[i][j] = (ver[i-1][j] if i>0else0) +1 ans =0for i in range(n):
for j in range(m):
for h in range(1, ver[i][j]+1):
for w in range(1, hor[i][j]+1):
r1 = i - h +1 c1 = j - w +1if ver[i][c1] >= h and hor[r1][j] >= w:
ans = max(ans, h * w)
return ans
⏰ Time complexity: O(n^4) worst-case — preprocessing is O(n*m), but enumerating bottom-right corners and all possible h,w yields quartic behavior for dense 1 matrices.