-
Notifications
You must be signed in to change notification settings - Fork 2
/
1139.最大的以 1 为边界的正方形.js
37 lines (36 loc) · 1.09 KB
/
1139.最大的以 1 为边界的正方形.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/**
* @param {number[][]} grid
* @return {number}
*/
var largest1BorderedSquare = function (grid) {
let result = 0;
const dp = [];
for (let i = 0; i < grid.length; i++) {
dp[i] = [];
for (let j = 0; j < grid[i].length; j++) {
dp[i][j] = [];
if (grid[i][j] === 0) {
dp[i][j][0] = 0;
dp[i][j][1] = 0;
} else if (i === 0) {
dp[i][j][0] = 1 + (j > 0 ? dp[i][j - 1][0] : 0);
dp[i][j][1] = 1;
} else if (j === 0) {
dp[i][j][0] = 1;
dp[i][j][1] = 1 + (i > 0 ? dp[i - 1][j][1] : 0);
} else {
dp[i][j][0] = 1 + dp[i][j - 1][0];
dp[i][j][1] = 1 + dp[i - 1][j][1];
}
let len = Math.min(...dp[i][j]);
while (len) {
if (dp[i - len + 1][j][0] >= len && dp[i][j - len + 1][1] >= len) {
break;
}
len--;
}
result = Math.max(result, len);
}
}
return result * result;
};