-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0764-largest-plus-sign.js
More file actions
103 lines (96 loc) · 3.04 KB
/
0764-largest-plus-sign.js
File metadata and controls
103 lines (96 loc) · 3.04 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/**
* Largest Plus Sign
* Time Complexity: O(n^2)
* Space Complexity: O(n^2)
*/
var orderOfLargestPlusSign = function (n, mines) {
const gridInitialized = Array(n)
.fill(0)
.map(() => Array(n).fill(1));
for (let currentMineLocation of mines) {
const mineRowCoordinate = currentMineLocation[0];
const mineColCoordinate = currentMineLocation[1];
gridInitialized[mineRowCoordinate][mineColCoordinate] = 0;
}
const leftConsecutiveOnes = Array(n)
.fill(0)
.map(() => Array(n).fill(0));
const upConsecutiveOnes = Array(n)
.fill(0)
.map(() => Array(n).fill(0));
for (
let rowIndexIterateForward = 0;
rowIndexIterateForward < n;
rowIndexIterateForward++
) {
for (
let colIndexIterateForward = 0;
colIndexIterateForward < n;
colIndexIterateForward++
) {
if (
gridInitialized[rowIndexIterateForward][colIndexIterateForward] === 1
) {
leftConsecutiveOnes[rowIndexIterateForward][colIndexIterateForward] =
(colIndexIterateForward > 0
? leftConsecutiveOnes[rowIndexIterateForward][
colIndexIterateForward - 1
]
: 0) + 1;
upConsecutiveOnes[rowIndexIterateForward][colIndexIterateForward] =
(rowIndexIterateForward > 0
? upConsecutiveOnes[rowIndexIterateForward - 1][
colIndexIterateForward
]
: 0) + 1;
}
}
}
const rightConsecutiveOnes = Array(n)
.fill(0)
.map(() => Array(n).fill(0));
const downConsecutiveOnes = Array(n)
.fill(0)
.map(() => Array(n).fill(0));
for (
let rowIndexIterateBackward = n - 1;
rowIndexIterateBackward >= 0;
rowIndexIterateBackward--
) {
for (
let colIndexIterateBackward = n - 1;
colIndexIterateBackward >= 0;
colIndexIterateBackward--
) {
if (
gridInitialized[rowIndexIterateBackward][colIndexIterateBackward] === 1
) {
rightConsecutiveOnes[rowIndexIterateBackward][colIndexIterateBackward] =
(colIndexIterateBackward < n - 1
? rightConsecutiveOnes[rowIndexIterateBackward][
colIndexIterateBackward + 1
]
: 0) + 1;
downConsecutiveOnes[rowIndexIterateBackward][colIndexIterateBackward] =
(rowIndexIterateBackward < n - 1
? downConsecutiveOnes[rowIndexIterateBackward + 1][
colIndexIterateBackward
]
: 0) + 1;
}
}
}
let largestPlusOrder = 0;
for (let finalRowIndex = 0; finalRowIndex < n; finalRowIndex++) {
for (let finalColIndex = 0; finalColIndex < n; finalColIndex++) {
const currentMinArmLength = Math.min(
leftConsecutiveOnes[finalRowIndex][finalColIndex],
rightConsecutiveOnes[finalRowIndex][finalColIndex],
upConsecutiveOnes[finalRowIndex][finalColIndex],
downConsecutiveOnes[finalRowIndex][finalColIndex],
);
largestPlusOrder = Math.max(largestPlusOrder, currentMinArmLength);
}
}
return largestPlusOrder;
};