add: 最大正方形

This commit is contained in:
2020-05-08 12:42:10 +08:00
parent 431ac87901
commit 3b8d9b3f40
3 changed files with 45 additions and 0 deletions

View File

@ -0,0 +1,25 @@
/**
* @param {character[][]} matrix
* @return {number}
*/
export const maximalSquare = function (matrix) {
if (!matrix || !matrix[0]) return 0
const rows = matrix.length
const cols = matrix[0].length
let max = 0
const dp = new Array(rows).fill().map(_ => new Array(cols).fill(0))
for (let n = 0; n < rows; n++) {
for (let i = 0; i < cols; i++) {
if (Number(matrix[n][i]) === 1) {
if (n === 0 || i === 0) dp[n][i] = 1
else dp[n][i] = Math.min(dp[n - 1][i], dp[n][i - 1], dp[n - 1][i - 1]) + 1 // 找规律
max = Math.max(max, dp[n][i])
}
}
}
return max * max
}