Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 224b7dd

Browse files
authored
Create Solution.java
1 parent 311166b commit 224b7dd

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
class Solution {
2+
public int largest1BorderedSquare(int[][] grid) {
3+
int m = grid.length, n = grid[0].length;
4+
int[][] down = new int[m][n];
5+
int[][] right = new int[m][n];
6+
for (int i = m - 1; i >= 0; --i) {
7+
for (int j = n - 1; j >= 0; --j) {
8+
if (grid[i][j] == 1) {
9+
down[i][j] += i + 1 < m ? down[i + 1][j] + 1 : 1;
10+
right[i][j] += j + 1 < n ? right[i][j + 1] + 1 : 1;
11+
}
12+
}
13+
}
14+
for (int len = Math.min(m, n); len > 0; --len) {
15+
for (int i = 0; i <= m - len; ++i) {
16+
for (int j = 0; j <= n - len; ++j) {
17+
if (down[i][j] >= len && right[i][j] >= len && right[i + len - 1][j] >= len && down[i][j + len - 1] >= len) {
18+
return len * len;
19+
}
20+
}
21+
}
22+
}
23+
return 0;
24+
}
25+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /