Skip to content

Commit b7a03e6

Browse files
authored
Create largest-1-bordered-square.cpp
1 parent 5c63c1c commit b7a03e6

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

C++/largest-1-bordered-square.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Time: O(n^3)
2+
// Space: O(n^2)
3+
4+
class Solution {
5+
public:
6+
int largest1BorderedSquare(vector<vector<int>>& grid) {
7+
auto left(grid), top(grid);
8+
for (int i = 0; i < grid.size(); ++i) {
9+
for (int j = 0; j < grid[0].size(); ++j) {
10+
if (!grid[i][j]) {
11+
continue;
12+
}
13+
if (i) {
14+
top[i][j] = top[i - 1][j] + 1;
15+
}
16+
if (j) {
17+
left[i][j] = left[i][j - 1] + 1;
18+
}
19+
}
20+
}
21+
for (int l = min(grid.size(), grid[0].size()); l >= 1; --l) {
22+
for (int i = 0; i <= grid.size() - l; ++i) {
23+
for (int j = 0; j <= grid[0].size() - l; ++j) {
24+
if (min({top[i + l - 1][j],
25+
top[i + l - 1][j + l - 1],
26+
left[i][j + l - 1],
27+
left[i + l - 1][j + l - 1]}) >= l) {
28+
return l * l;
29+
}
30+
}
31+
}
32+
}
33+
return 0;
34+
}
35+
};

0 commit comments

Comments
 (0)