101 Logo
onenoughtone

Code Implementation

Largest Plus Sign Finder Implementation

Below is the implementation of the largest plus sign finder:

solution.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Find the order of the largest plus sign in a grid with buildings.
* Dynamic Programming approach.
*
* @param {number} n - The size of the grid
* @param {number[][]} mines - The coordinates of buildings
* @return {number} - The order of the largest plus sign
*/
function orderOfLargestPlusSign(n, mines) {
// Initialize grid with all 1's
const grid = Array(n).fill().map(() => Array(n).fill(1));
// Mark buildings as 0's
for (const [x, y] of mines) {
grid[x][y] = 0;
}
// Initialize arrays to store consecutive 1's in each direction
const left = Array(n).fill().map(() => Array(n).fill(0));
const right = Array(n).fill().map(() => Array(n).fill(0));
const up = Array(n).fill().map(() => Array(n).fill(0));
const down = Array(n).fill().map(() => Array(n).fill(0));
// Compute left and right arrays
for (let i = 0; i < n; i++) {
// Left to right
for (let j = 0; j < n; j++) {
if (grid[i][j] === 1) {
left[i][j] = (j > 0) ? left[i][j - 1] + 1 : 1;
}
}
// Right to left
for (let j = n - 1; j >= 0; j--) {
if (grid[i][j] === 1) {
right[i][j] = (j < n - 1) ? right[i][j + 1] + 1 : 1;
}
}
}
// Compute up and down arrays
for (let j = 0; j < n; j++) {
// Top to bottom
for (let i = 0; i < n; i++) {
if (grid[i][j] === 1) {
up[i][j] = (i > 0) ? up[i - 1][j] + 1 : 1;
}
}
// Bottom to top
for (let i = n - 1; i >= 0; i--) {
if (grid[i][j] === 1) {
down[i][j] = (i < n - 1) ? down[i + 1][j] + 1 : 1;
}
}
}
// Find the largest plus sign
let maxOrder = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
const order = Math.min(left[i][j], right[i][j], up[i][j], down[i][j]);
maxOrder = Math.max(maxOrder, order);
}
}
return maxOrder;
}
/**
* Find the order of the largest plus sign in a grid with buildings.
* Optimized Dynamic Programming approach.
*
* @param {number} n - The size of the grid
* @param {number[][]} mines - The coordinates of buildings
* @return {number} - The order of the largest plus sign
*/
function orderOfLargestPlusSignOptimized(n, mines) {
// Initialize dp array with maximum possible order
const dp = Array(n).fill().map(() => Array(n).fill(n));
// Create a set to store building coordinates
const buildings = new Set();
// Mark buildings
for (const [x, y] of mines) {
buildings.add(x * n + y);
dp[x][y] = 0;
}
// Process each row and column
for (let i = 0; i < n; i++) {
// Left to right
let count = 0;
for (let j = 0; j < n; j++) {
count = buildings.has(i * n + j) ? 0 : count + 1;
dp[i][j] = Math.min(dp[i][j], count);
}
// Right to left
count = 0;
for (let j = n - 1; j >= 0; j--) {
count = buildings.has(i * n + j) ? 0 : count + 1;
dp[i][j] = Math.min(dp[i][j], count);
}
// Top to bottom
count = 0;
for (let j = 0; j < n; j++) {
count = buildings.has(j * n + i) ? 0 : count + 1;
dp[j][i] = Math.min(dp[j][i], count);
}
// Bottom to top
count = 0;
for (let j = n - 1; j >= 0; j--) {
count = buildings.has(j * n + i) ? 0 : count + 1;
dp[j][i] = Math.min(dp[j][i], count);
}
}
// Find the maximum order
let maxOrder = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
maxOrder = Math.max(maxOrder, dp[i][j]);
}
}
return maxOrder;
}
/**
* Find the order of the largest plus sign in a grid with buildings.
* Brute Force approach.
*
* @param {number} n - The size of the grid
* @param {number[][]} mines - The coordinates of buildings
* @return {number} - The order of the largest plus sign
*/
function orderOfLargestPlusSignBruteForce(n, mines) {
// Initialize grid with all 1's
const grid = Array(n).fill().map(() => Array(n).fill(1));
// Mark buildings as 0's
for (const [x, y] of mines) {
grid[x][y] = 0;
}
// Find the largest plus sign
let maxOrder = 0;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
if (grid[i][j] === 0) continue;
let order = 0;
// Expand outward from the center
while (true) {
const k = order + 1;
// Check if all cells at distance k in all four directions are valid
if (i - k >= 0 && i + k < n && j - k >= 0 && j + k < n &&
grid[i - k][j] === 1 && grid[i + k][j] === 1 &&
grid[i][j - k] === 1 && grid[i][j + k] === 1) {
order++;
} else {
break;
}
}
maxOrder = Math.max(maxOrder, order + 1);
}
}
return maxOrder;
}
// Test cases
console.log(orderOfLargestPlusSign(5, [[4, 2]])); // 2
console.log(orderOfLargestPlusSign(3, [[0, 0], [0, 2], [2, 0], [2, 2]])); // 1
console.log(orderOfLargestPlusSign(2, [[0, 0], [0, 1], [1, 0], [1, 1]])); // 0
console.log(orderOfLargestPlusSignOptimized(5, [[4, 2]])); // 2
console.log(orderOfLargestPlusSignOptimized(3, [[0, 0], [0, 2], [2, 0], [2, 2]])); // 1
console.log(orderOfLargestPlusSignOptimized(2, [[0, 0], [0, 1], [1, 0], [1, 1]])); // 0
console.log(orderOfLargestPlusSignBruteForce(5, [[4, 2]])); // 2
console.log(orderOfLargestPlusSignBruteForce(3, [[0, 0], [0, 2], [2, 0], [2, 2]])); // 1
console.log(orderOfLargestPlusSignBruteForce(2, [[0, 0], [0, 1], [1, 0], [1, 1]])); // 0

Step-by-Step Explanation

Let's break down the implementation:

  1. Understand the Problem: First, understand that we need to find the order of the largest plus sign that can be placed in a grid with buildings.
  2. Initialize the Grid: Create a grid of size n×n with all cells set to 1, and mark the cells corresponding to buildings as 0.
  3. Compute Consecutive 1's: For each cell, compute the number of consecutive 1's in all four directions (up, down, left, right).
  4. Find the Largest Plus Sign: For each cell, the order of the largest plus sign centered at that cell is the minimum of the four values computed in the previous step.
  5. Return the Maximum Order: Return the maximum order found among all cells.
ProblemSolutionCode
101 Logo
onenoughtone