Skip to main content
Back to problems
#764
Medium Algorithms

Largest plus sign

Array Dynamic Programming
49.1% acceptance
Feb 21, 2026
1536
241
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.

Solution

Rust
Time O(n * m)
Space O(n * m)
LeetCode
solution.rs
/*
 * 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 <= 500
 * 1 <= mines.length <= 5000
 * 0 <= xi, yi < n
 * All the pairs (xi, yi) are unique.
 */
use std::collections::HashSet;

impl Solution {
  pub fn order_of_largest_plus_sign(n: i32, mines: Vec<Vec<i32>>) -> i32 {
    let n = n as usize;
    let mine_set: HashSet<(i32, i32)> = mines.iter().map(|m| (m[0], m[1])).collect();
    let mut dp = vec![vec![n as i32; n]; n];

    for r in 0..n {
      let mut cnt = 0i32;
      for c in 0..n {
        cnt = if mine_set.contains(&(r as i32, c as i32)) { 0 } else { cnt + 1 };
        dp[r][c] = dp[r][c].min(cnt);
      }
      cnt = 0;
      for c in (0..n).rev() {
        cnt = if mine_set.contains(&(r as i32, c as i32)) { 0 } else { cnt + 1 };
        dp[r][c] = dp[r][c].min(cnt);
      }
    }
    for c in 0..n {
      let mut cnt = 0i32;
      for r in 0..n {
        cnt = if mine_set.contains(&(r as i32, c as i32)) { 0 } else { cnt + 1 };
        dp[r][c] = dp[r][c].min(cnt);
      }
      cnt = 0;
      for r in (0..n).rev() {
        cnt = if mine_set.contains(&(r as i32, c as i32)) { 0 } else { cnt + 1 };
        dp[r][c] = dp[r][c].min(cnt);
      }
    }

    dp.iter().flat_map(|row| row.iter()).copied().max().unwrap_or(0)
  }
}