-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaximalSquare.js
31 lines (29 loc) · 1.07 KB
/
maximalSquare.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
/**
* @param {character[][]} matrix
* @return {number}
*/
var maximalSquare = function(matrix) {
let maxDiagonal = 0;
let possibleSquare = [];
for(let itrRow = 0; itrRow < matrix.length; itrRow++) {
possibleSquare[itrRow%2] = [];
for(let itrCol = 0; itrCol < matrix[0].length; itrCol++) {
let value = matrix[itrRow][itrCol];
if(value == 0) {
possibleSquare[itrRow%2][itrCol] = 0;
} else if(itrRow == 0 || itrCol == 0){
possibleSquare[itrRow%2][itrCol] = value;
} else {
let minValue = Math.min(
possibleSquare[(itrRow-1)%2][itrCol-1],
possibleSquare[itrRow%2][itrCol-1],
possibleSquare[(itrRow-1)%2][itrCol]
) + 1;
possibleSquare[itrRow%2][itrCol] = minValue;
}
maxDiagonal = Math.max(maxDiagonal, possibleSquare[itrRow%2][itrCol]);
}
}
return maxDiagonal * maxDiagonal;
};
//https://leetcode.com/problems/maximal-square/