Watch 10 video solutions for Largest Plus Sign, a medium level problem involving Array, Dynamic Programming. This walkthrough by Coding Decoded has 3,644 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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.Problem Overview: You are given an n x n grid where some cells contain mines. A plus sign consists of a center cell and four arms (up, down, left, right) of equal length made only of 1s. The goal is to return the order of the largest possible plus sign that can exist in the grid.
Approach 1: Greedy Scanning with Set Usage (O(n^3) time, O(m) space)
The straightforward solution checks every cell as a potential center of a plus sign. Store all mine positions inside a set for constant-time lookup. For each cell that is not a mine, expand outward in four directions while ensuring you don't hit a mine or the grid boundary. The smallest arm length across the four directions determines the order of the plus sign centered at that cell. This approach is simple to reason about but inefficient because every expansion can scan up to O(n) cells for each of the n^2 grid positions.
Approach 2: Dynamic Programming Directional Scan (O(n^2) time, O(n^2) space)
The optimized solution uses Dynamic Programming to precompute the number of consecutive non-mine cells in all four directions for every position. Start with a grid initialized to n, then mark mine positions as 0. Perform four directional scans: left-to-right, right-to-left, top-to-bottom, and bottom-to-top. During each scan, track the running streak of valid cells and update the current cell with the minimum value seen so far. The final value at each cell represents the maximum order of a plus sign centered there. The answer is the maximum value across the grid.
This technique works because a valid plus sign requires equal-length arms in all four directions. Taking the minimum of the directional counts guarantees the limiting arm length is respected. The algorithm touches each cell a constant number of times, which reduces the complexity from cubic to quadratic.
The grid manipulation relies heavily on sequential passes over a matrix, which is common in problems involving arrays and directional preprocessing.
Recommended for interviews: The dynamic programming directional scan is the expected solution. The greedy expansion approach demonstrates understanding of the problem but scales poorly. Interviewers typically look for the DP insight that precomputes directional streaks and combines them with a min operation to evaluate each center in constant time.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Greedy Expansion with Mine Set | O(n^3) | O(m) | Simple baseline approach or when grid size is small |
| Dynamic Programming Directional Scan | O(n^2) | O(n^2) | Optimal solution for large grids and expected interview approach |