3
* @param {number[][]} mines6
var orderOfLargestPlusSign = function (n, mines) {7
// create n * n as a table, and each grid fill the maximum number8
const t = new Array(n).fill(0).map(() => new Array(n).fill(n));10
// insert `0` to the grid in table according the position in mines11
mines.forEach((a) => (t[a[0]][a[1]] = 0));13
// loop each rows and cols14
// - for rows: calculate non-zero grid from left to right15
// - for cols: calculate non-zero grid from top to bottom16
// - when the loop is completed, all of non-zero values will be calculated by four directions17
// - and these grids value will be updated by comparing. then we can obtain the minimum value of the four directions caculation, which is the maximum of the grid18
for (let i = 0; i < n; i++) {19
// save the maximum value of non-zeros with for directions20
let [l, r, u, d] = [0, 0, 0, 0];22
// l: left, loop row i from left to right23
// r: right, loop row i from right to left24
// u: up, loop col i from top to bottom25
// d: down, loop col i from bottom to top26
for (let j = 0, k = n - 1; j < n; j++, k--) {27
// if the value is `0`, then set variable to `0`, and indicates it's broken33
// if current value is less than origin value34
// one possibility: the origin value is default35
// another possibility: the length of non-zero in a centain direction is langer than in the current direction, which the minimum value we need36
if (l < t[i][j]) t[i][j] = l;37
if (r < t[i][k]) t[i][k] = r;38
if (u < t[j][i]) t[j][i] = u;39
if (d < t[k][i]) t[k][i] = d;43
// return maximum value be saved by all cells44
return Math.max(...t.map((v) => Math.max(...v)));