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.
What if the matrix is not N×N but M×N? What if the matrix is very sparse (only P ones, with P << M*N)? OR What if we also need to return the coordinates of the center of the plus/cross sign. Here is the answer to it: : Largest cross in a sparse matrix.
For each cell, the order of the plus sign is determined by the minimum number of consecutive 1’s in all four directions (up, down, left, right). We precompute these values using dynamic programming.
classSolution {
public:int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
vector<vector<int>> grid(n, vector<int>(n, n));
for (auto& m : mines) grid[m[0]][m[1]] =0;
for (int i =0; i < n; ++i) {
int l =0, r =0;
for (int j =0, k = n-1; j < n; ++j, --k) {
l = grid[i][j] ? l+1:0;
grid[i][j] = min(grid[i][j], l);
r = grid[i][k] ? r+1:0;
grid[i][k] = min(grid[i][k], r);
}
}
for (int j =0; j < n; ++j) {
int u =0, d =0;
for (int i =0, k = n-1; i < n; ++i, --k) {
u = grid[i][j] ? u+1:0;
grid[i][j] = min(grid[i][j], u);
d = grid[k][j] ? d+1:0;
grid[k][j] = min(grid[k][j], d);
}
}
int ans =0;
for (int i =0; i < n; ++i)
for (int j =0; j < n; ++j)
ans = max(ans, grid[i][j]);
return ans;
}
};
classSolution {
funorderOfLargestPlusSign(n: Int, mines: Array<IntArray>): Int {
val grid = Array(n) { IntArray(n) { n } }
for (m in mines) grid[m[0]][m[1]] = 0for (i in0 until n) {
var l = 0; var r = 0for (j in0 until n) {
l = if (grid[i][j] !=0) l+1else0 grid[i][j] = minOf(grid[i][j], l)
val k = n-1-j
r = if (grid[i][k] !=0) r+1else0 grid[i][k] = minOf(grid[i][k], r)
}
}
for (j in0 until n) {
var u = 0; var d = 0for (i in0 until n) {
u = if (grid[i][j] !=0) u+1else0 grid[i][j] = minOf(grid[i][j], u)
val k = n-1-i
d = if (grid[k][j] !=0) d+1else0 grid[k][j] = minOf(grid[k][j], d)
}
}
var ans = 0for (i in0 until n)
for (j in0 until n)
ans = maxOf(ans, grid[i][j])
return ans
}
}
classSolution:
deforderOfLargestPlusSign(self, n: int, mines: list[list[int]]) -> int:
grid = [[n]*n for _ in range(n)]
for x, y in mines:
grid[x][y] =0for i in range(n):
l = r =0for j in range(n):
l = l+1if grid[i][j] else0 grid[i][j] = min(grid[i][j], l)
k = n-1-j
r = r+1if grid[i][k] else0 grid[i][k] = min(grid[i][k], r)
for j in range(n):
u = d =0for i in range(n):
u = u+1if grid[i][j] else0 grid[i][j] = min(grid[i][j], u)
k = n-1-i
d = d+1if grid[k][j] else0 grid[k][j] = min(grid[k][j], d)
ans =0for i in range(n):
for j in range(n):
ans = max(ans, grid[i][j])
return ans
⏰ Time complexity: O(n^2) – Each cell participates in four directional passes (two row + two column sweeps).
🧺 Space complexity: O(n^2) – Stores the compressed directional minima in-place (can be reduced to O(n) auxiliary if four separate arrays are streamed).
Extended Methods (2 & 3) with full code have been moved under the Follow up section to keep the primary LeetCode solution concise.
⏰ Time complexity: O(n^2) – Each cell participates in four directional passes (two row + two column sweeps).
🧺 Space complexity: O(n^2) – Stores the compressed directional minima in-place (can be reduced to O(n) auxiliary if four separate arrays are streamed).