You are given an integer n. You have an n x n binary grid grid with all values initially 1's except for some indices given in the array mines. The ith element of the array mines is defined as mines[i] = [xi, yi] where grid[xi][yi] == 0.
Return the order of the largest axis-aligned plus sign of 1's contained in grid. If there is none, return 0.
An axis-aligned plus sign of 1's of order k has some center grid[r][c] == 1 along with four arms of length k - 1 going up, down, left, and right, and made of 1's. Note that there could be 0's or 1's beyond the arms of the plus sign, only the relevant area of the plus sign is checked for 1's.
Example 1:
Input: n = 5, mines = [[4,2]] Output: 2 Explanation: In the above grid, the largest plus sign can only be of order 2. One of them is shown.
Example 2:
Input: n = 1, mines = [[0,0]] Output: 0 Explanation: There is no plus sign, so return 0.
Constraints:
1 <= n <= 5001 <= mines.length <= 50000 <= xi, yi < n(xi, yi) are unique.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
We're looking for the biggest plus sign we can make on a grid, but some spots are blocked. The brute force way is to try making a plus at every possible spot and see how big we can make it.
Here's how the algorithm would work step-by-step:
def largest_plus_sign_brute_force(grid_size, mines):
grid = [[1] * grid_size for _ in range(grid_size)]
# Mark the mined cells as 0
for row, col in mines:
grid[row][col] = 0
max_order = 0
for row in range(grid_size):
for col in range(grid_size):
if grid[row][col] == 1:
order = 0
can_expand = True
# Increase plus sign size while possible
while can_expand:
order += 1
# Check if the plus sign extends beyond grid bounds or hits a mine
if (row - order < 0 or row + order >= grid_size or\
col - order < 0 or col + order >= grid_size or\
grid[row - order][col] == 0 or grid[row + order][col] == 0 or\
grid[row][col - order] == 0 or grid[row][col + order] == 0):
# Stop expanding if we hit a mine or edge
can_expand = False
order -= 1
# Update the maximum order found so far
max_order = max(max_order, order)
return max_orderThe goal is to find the largest 'plus' shape of 1s within a grid, avoiding certain blocked cells. Instead of checking every possible plus shape size and location individually (which would be slow), we calculate the maximum possible 'arm length' extending in each of the four directions from every cell efficiently.
Here's how the algorithm would work step-by-step:
def largest_plus_sign(grid_size, mines):
grid = [[1] * grid_size for _ in range(grid_size)]
for row, col in mines:
grid[row][col] = 0
left = [[0] * grid_size for _ in range(grid_size)]
right = [[0] * grid_size for _ in range(grid_size)]
up = [[0] * grid_size for _ in range(grid_size)]
down = [[0] * grid_size for _ in range(grid_size)]
for row_index in range(grid_size):
for col_index in range(grid_size):
if grid[row_index][col_index] == 1:
left[row_index][col_index] = (left[row_index][col_index - 1] + 1
if col_index > 0 else 1)
up[row_index][col_index] = (up[row_index - 1][col_index] + 1
if row_index > 0 else 1)
for row_index in range(grid_size - 1, -1, -1):
for col_index in range(grid_size - 1, -1, -1):
if grid[row_index][col_index] == 1:
right[row_index][col_index] = (right[row_index][col_index + 1] + 1
if col_index < grid_size - 1 else 1)
down[row_index][col_index] = (down[row_index + 1][col_index] + 1
if row_index < grid_size - 1 else 1)
max_plus_order = 0
for row_index in range(grid_size):
for col_index in range(grid_size):
# Find min of arm lengths to determine plus size
order = min(left[row_index][col_index],
right[row_index][col_index],
up[row_index][col_index],
down[row_index][col_index])
# Track the maximum plus size seen so far.
max_plus_order = max(max_plus_order, order)
return max_plus_order| Case | How to Handle |
|---|---|
| N is zero or negative | Return 0 since a plus sign of order 0 doesn't exist and negative size is invalid. |
| mines is null or empty | Return N (the maximum possible order) since no cells are forbidden. |
| mines contains out-of-bounds coordinates | Filter out invalid coordinates before processing or handle them gracefully by treating them as forbidden cells without throwing an exception. |
| N is 1 and mines is empty | Return 1, as the entire grid consists of a single valid cell. |
| All cells are in mines | Return 0 since no valid plus sign of order 1 or greater can be formed. |
| Large N value that may cause integer overflow when calculating distances or dimensions | Use appropriate data types (e.g., long) to prevent integer overflow during calculations of distances or dimensions. |
| mines contains duplicate coordinates | The solution will overwrite duplicated mines; this behavior should be consistent with the problem definition (if duplicates are present treat as a single mine). |
| The mines configuration results in a single plus sign in the entire board, centered exactly in the middle | The algorithm should correctly identify and return the order of this single, isolated plus sign. |