File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change
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
+ };
You can’t perform that action at this time.
0 commit comments