forked from lazzzis/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
62 lines (62 loc) · 1.33 KB
/
main.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/**
* @param {number} N
* @param {number[][]} mines
* @return {number}
*/
var orderOfLargestPlusSign = function (N, mines) {
const zeros = Array.from({length: N},
() => Array.from({length: N}, () => false))
for (const mine of mines) {
zeros[mine[0]][mine[1]] = true
}
const nearest = Array.from({length: N},
() => Array.from({length: N}, () => ({
top: -1, left: -1, right: N, bottom: N
})))
for (let i = 0; i < N; i++) {
let left = -1
let top = -1
for (let j = 0; j < N; j++) {
if (zeros[i][j]) {
left = j
} else {
nearest[i][j].left = left
}
if (zeros[j][i]) {
top = j
} else {
nearest[j][i].top = top
}
}
let right = N
let bottom = N
for (let j = N - 1; j >= 0; j--) {
if (zeros[i][j]) {
right = j
} else {
nearest[i][j].right = right
}
if (zeros[j][i]) {
bottom = j
} else {
nearest[j][i].bottom = bottom
}
}
}
let ans = 0
for (let i = 0; i < N; i++) {
for (let j = 0; j < N; j++) {
if (zeros[i][j]) continue
ans = Math.max(
ans,
Math.min(
j - nearest[i][j].left,
nearest[i][j].right - j,
i - nearest[i][j].top,
nearest[i][j].bottom - i,
)
)
}
}
return ans
}