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 - DP Approach INPUT 5x5 Binary Grid (n=5) 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 n = 5 mines = [[4,2]] Red cell = mine (0) ALGORITHM STEPS 1 Initialize Grid Set all cells to 1, mines to 0 2 Compute 4 DP Arrays left, right, up, down counts 3 Find Min of 4 Directions For each cell: min(L,R,U,D) 4 Track Maximum Return largest plus order DP at cell (2,2): 2,2 U:3 D:2 L:3 R:3 min(3,3,3,2) = 2 FINAL RESULT Largest Plus Sign (order 2) CTR 0 Output 2 Order 2: center + arms of length 1 in 4 dirs OK - Verified Key Insight: For each cell, precompute consecutive 1's in all 4 directions using DP. The plus order at any cell is min(left, right, up, down) - the bottleneck arm determines the size. Time: O(n^2) | Space: O(n^2) - much better than brute force O(n^3)! TutorialsPoint - Largest Plus Sign | Dynamic Programming Approach
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