hot100
May 13, 2024Less than 1 minute
function maximalSquare(matrix: string[][]): number {
let numberMatrix = Array.from({ length: matrix.length }, () =>
Array.from({ length: matrix[0].length }, () => 0)
);
for (let i = 0; i < numberMatrix.length; i++) {
for (let j = 0; j < numberMatrix[0].length; j++) {
if (i == 0 || j == 0) {
numberMatrix[i][j] = parseInt(matrix[i][j]);
} else if (matrix[i][j] == "1") {
numberMatrix[i][j] =
Math.min(
numberMatrix[i - 1][j],
numberMatrix[i - 1][j - 1],
numberMatrix[i][j - 1]
) + 1;
}
}
}
return Math.max(...numberMatrix.flat()) ** 2;
}