Largest Valid Plus Sign in a Grid

You are given an integer n, representing the size of an n x n grid.

Initially, every cell in the grid contains 1.

However, some cells are blocked and contain 0. These blocked cells are given in the array mines, where mines[i] = [rowi, coli].

Your task is to return the order of the largest axis-aligned plus sign made only of 1s.

A plus sign of order k means:

  • Its center cell is 1
  • It has four arms extending: up, down, left, right
  • Each arm has length k - 1
  • Every cell used in the center and all four arms must contain 1

Only the cells that belong to the plus sign matter. Cells outside the plus sign can be either 0 or 1.

Return the order of the largest possible plus sign.

If no valid plus sign exists, return 0.

Example 1:

Input: n = 5, mines = [[4,2]]

Output: 2

Explanation:

After marking cell (4,2) as 0, the grid still contains plus signs of order 2.

No plus sign of order 3 can be formed.

Example 2:

Input: n = 3, mines = [[0,0],[0,1],[0,2],[1,0],[1,2],[2,0],[2,1],[2,2]]

Output: 1

Explanation:

Only the center cell (1,1) remains as 1.

So the largest valid plus sign has only the center and no arms, which means its order is 1.

Now Your Turn!

Pick the correct output for the given input

Input: n = 3, mines = [[1,1]]

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= n <= 500
  • 1 <= mines.length <= min(5000, n * n)
  • mines[i].length == 2
  • 0 <= mines[i][0], mines[i][1] < n
  • All blocked cell positions in mines are unique

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution {
public:
int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
// Your code goes here
}
};
 
Test Case

Input:

Mines
N