Largest Plus Sign - Problem

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 i-th element of the array mines is defined as mines[i] = [x_i, y_i] where grid[x_i][y_i] == 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.

Input & Output

Example 1 — Basic Grid
$ Input: n = 5, mines = [[4,2]]
Output: 2
💡 Note: Grid is 5×5 with all 1's except mines at (4,2). The largest plus sign has order 2, can be centered at multiple positions like (1,2), (2,2), etc.
Example 2 — Single Cell
$ Input: n = 1, mines = []
Output: 1
💡 Note: 1×1 grid with single cell containing 1. The plus sign order is 1 (just the center, no arms).
Example 3 — All Mines
$ Input: n = 1, mines = [[0,0]]
Output: 0
💡 Note: Only cell is a mine (0), so no plus sign can be formed.

Constraints

  • 1 ≤ n ≤ 500
  • 1 ≤ mines.length ≤ 5000
  • 0 ≤ xi, yi < n
  • All the pairs (xi, yi) are unique.

Visualization

Tap to expand
Largest Plus Sign Problem5×5 Grid with mine at (4,2):11111C0Plus sign of order 2:• Center: (2,2) = 1• Up arm: 1 cell• Down arm: 1 cell• Left arm: 1 cell• Right arm: 1 cellMaximum Plus Order: 2= 1 (valid)= 0 (mine)= center= plus arm
Understanding the Visualization
1
Input Grid
n×n grid with 1's except at mine positions
2
Find Plus Signs
Check each cell as potential plus center
3
Calculate Order
Order = min(arm lengths in 4 directions) + 1
Key Takeaway
🎯 Key Insight: Pre-compute consecutive 1's from each direction to avoid O(n⁴) redundant checking
Asked in
Google 15 Facebook 8
28.0K Views
Medium Frequency
~35 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen