101 Logo
onenoughtone

Solution Approach

Solution Overview

There are 3 main approaches to solve this problem:

  1. Dynamic Programming Approach - Complex approach
  2. Optimized Dynamic Programming - Complex approach
  3. Brute Force Approach - Complex approach

Approach 1: Dynamic Programming Approach

The key insight for solving this problem is to precompute the number of consecutive 1's in each of the four directions (up, down, left, right) for each cell in the grid. Then, for each cell, the order of the largest plus sign centered at that cell is the minimum of these four values.

We can use dynamic programming to efficiently compute these values in a single pass through the grid for each direction.

Algorithm:

  1. Initialize a 2D grid of size n×n with all cells set to 1.
  2. Mark the cells corresponding to the buildings (mines) as 0.
  3. Initialize four 2D arrays (left, right, up, down) to store the number of consecutive 1's in each direction for each cell.
  4. For each row, compute the left and right arrays:
  5. a. For left, scan from left to right and set left[i][j] = 0 if grid[i][j] = 0, otherwise left[i][j] = left[i][j-1] + 1.
  6. b. For right, scan from right to left and set right[i][j] = 0 if grid[i][j] = 0, otherwise right[i][j] = right[i][j+1] + 1.
  7. For each column, compute the up and down arrays:
  8. a. For up, scan from top to bottom and set up[i][j] = 0 if grid[i][j] = 0, otherwise up[i][j] = up[i-1][j] + 1.
  9. b. For down, scan from bottom to top and set down[i][j] = 0 if grid[i][j] = 0, otherwise down[i][j] = down[i+1][j] + 1.
  10. For each cell, compute the order of the largest plus sign centered at that cell as the minimum of left[i][j], right[i][j], up[i][j], and down[i][j].
  11. Return the maximum order found.

Time Complexity:

O(n²)

Where n is the size of the grid. We need to process each cell in the grid a constant number of times.

Space Complexity:

O(n²)

We need to store four 2D arrays of size n×n to keep track of the number of consecutive 1's in each direction.

Approach 2: Optimized Dynamic Programming

We can optimize the space complexity of the previous approach by using a single 2D array to store the minimum of the four values for each cell, instead of keeping four separate arrays.

The idea is to compute the values for each direction on the fly and update the minimum for each cell as we go.

Algorithm:

  1. Initialize a 2D array dp of size n×n with all cells set to n (the maximum possible order).
  2. Create a set to store the coordinates of the buildings (mines).
  3. For each cell (i, j) that is a building, set dp[i][j] = 0.
  4. For each row i:
  5. a. Initialize count = 0 and scan from left to right:
  6. i. If (i, j) is a building, set count = 0, otherwise increment count.
  7. ii. Update dp[i][j] = min(dp[i][j], count).
  8. b. Reset count = 0 and scan from right to left:
  9. i. If (i, j) is a building, set count = 0, otherwise increment count.
  10. ii. Update dp[i][j] = min(dp[i][j], count).
  11. For each column j:
  12. a. Initialize count = 0 and scan from top to bottom:
  13. i. If (i, j) is a building, set count = 0, otherwise increment count.
  14. ii. Update dp[i][j] = min(dp[i][j], count).
  15. b. Reset count = 0 and scan from bottom to top:
  16. i. If (i, j) is a building, set count = 0, otherwise increment count.
  17. ii. Update dp[i][j] = min(dp[i][j], count).
  18. Return the maximum value in the dp array.

Time Complexity:

O(n²)

Where n is the size of the grid. We still need to process each cell in the grid a constant number of times.

Space Complexity:

O(n²)

We only need one 2D array of size n×n to store the minimum values, plus a set to store the coordinates of the buildings.

Approach 3: Brute Force Approach

A simpler but less efficient approach is to check each possible plus sign centered at each cell in the grid.

For each cell, we can expand outward and check how far we can go in each direction before hitting a building or the edge of the grid.

Algorithm:

  1. Initialize a 2D grid of size n×n with all cells set to 1.
  2. Mark the cells corresponding to the buildings (mines) as 0.
  3. Initialize maxOrder = 0 to keep track of the largest plus sign found.
  4. For each cell (i, j) in the grid:
  5. a. If grid[i][j] = 0, skip this cell (it's a building).
  6. b. Initialize order = 0 and expand outward from the cell:
  7. i. While all cells at distance order+1 in all four directions are valid (within the grid and not buildings), increment order.
  8. c. Update maxOrder = max(maxOrder, order).
  9. Return maxOrder.

Time Complexity:

O(n³)

Where n is the size of the grid. For each of the n² cells, we might need to check up to n cells in each direction.

Space Complexity:

O(n²)

We need to store the grid of size n×n.

Pseudocode

solution.pseudo
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
function orderOfLargestPlusSign(n, mines):
// Initialize grid with all 1's
grid = 2D array of size n×n, initialized with 1's
// Mark buildings as 0's
for each [x, y] in mines:
grid[x][y] = 0
// Initialize arrays to store consecutive 1's in each direction
left = 2D array of size n×n, initialized with 0's
right = 2D array of size n×n, initialized with 0's
up = 2D array of size n×n, initialized with 0's
down = 2D array of size n×n, initialized with 0's
// Compute left and right arrays
for i from 0 to n-1:
// Left to right
for j from 0 to n-1:
if grid[i][j] == 1:
left[i][j] = (j > 0) ? left[i][j-1] + 1 : 1
// Right to left
for j from n-1 down to 0:
if grid[i][j] == 1:
right[i][j] = (j < n-1) ? right[i][j+1] + 1 : 1
// Compute up and down arrays
for j from 0 to n-1:
// Top to bottom
for i from 0 to n-1:
if grid[i][j] == 1:
up[i][j] = (i > 0) ? up[i-1][j] + 1 : 1
// Bottom to top
for i from n-1 down to 0:
if grid[i][j] == 1:
down[i][j] = (i < n-1) ? down[i+1][j] + 1 : 1
// Find the largest plus sign
maxOrder = 0
for i from 0 to n-1:
for j from 0 to n-1:
order = min(left[i][j], right[i][j], up[i][j], down[i][j])
maxOrder = max(maxOrder, order)
return maxOrder
ProblemSolutionCode
101 Logo
onenoughtone