Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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 summary: 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.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
5 [[4,2]]
1 [[0,0]]
maximal-square)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #764: Largest Plus Sign
class Solution {
public int orderOfLargestPlusSign(int n, int[][] mines) {
int[][] dp = new int[n][n];
for (var e : dp) {
Arrays.fill(e, n);
}
for (var e : mines) {
dp[e[0]][e[1]] = 0;
}
for (int i = 0; i < n; ++i) {
int left = 0, right = 0, up = 0, down = 0;
for (int j = 0, k = n - 1; j < n; ++j, --k) {
left = dp[i][j] > 0 ? left + 1 : 0;
right = dp[i][k] > 0 ? right + 1 : 0;
up = dp[j][i] > 0 ? up + 1 : 0;
down = dp[k][i] > 0 ? down + 1 : 0;
dp[i][j] = Math.min(dp[i][j], left);
dp[i][k] = Math.min(dp[i][k], right);
dp[j][i] = Math.min(dp[j][i], up);
dp[k][i] = Math.min(dp[k][i], down);
}
}
return Arrays.stream(dp).flatMapToInt(Arrays::stream).max().getAsInt();
}
}
// Accepted solution for LeetCode #764: Largest Plus Sign
func orderOfLargestPlusSign(n int, mines [][]int) (ans int) {
dp := make([][]int, n)
for i := range dp {
dp[i] = make([]int, n)
for j := range dp[i] {
dp[i][j] = n
}
}
for _, e := range mines {
dp[e[0]][e[1]] = 0
}
for i := 0; i < n; i++ {
var left, right, up, down int
for j, k := 0, n-1; j < n; j, k = j+1, k-1 {
left, right, up, down = left+1, right+1, up+1, down+1
if dp[i][j] == 0 {
left = 0
}
if dp[i][k] == 0 {
right = 0
}
if dp[j][i] == 0 {
up = 0
}
if dp[k][i] == 0 {
down = 0
}
dp[i][j] = min(dp[i][j], left)
dp[i][k] = min(dp[i][k], right)
dp[j][i] = min(dp[j][i], up)
dp[k][i] = min(dp[k][i], down)
}
}
for _, e := range dp {
ans = max(ans, slices.Max(e))
}
return
}
# Accepted solution for LeetCode #764: Largest Plus Sign
class Solution:
def orderOfLargestPlusSign(self, n: int, mines: List[List[int]]) -> int:
dp = [[n] * n for _ in range(n)]
for x, y in mines:
dp[x][y] = 0
for i in range(n):
left = right = up = down = 0
for j, k in zip(range(n), reversed(range(n))):
left = left + 1 if dp[i][j] else 0
right = right + 1 if dp[i][k] else 0
up = up + 1 if dp[j][i] else 0
down = down + 1 if dp[k][i] else 0
dp[i][j] = min(dp[i][j], left)
dp[i][k] = min(dp[i][k], right)
dp[j][i] = min(dp[j][i], up)
dp[k][i] = min(dp[k][i], down)
return max(max(v) for v in dp)
// Accepted solution for LeetCode #764: Largest Plus Sign
struct Solution;
impl Solution {
fn order_of_largest_plus_sign(n: i32, mines: Vec<Vec<i32>>) -> i32 {
let n = n as usize;
let mut grid = vec![vec![1; n]; n];
let mut left = vec![vec![0; n]; n];
let mut top = vec![vec![0; n]; n];
let mut right = vec![vec![0; n]; n];
let mut bottom = vec![vec![0; n]; n];
for mine in mines {
let i = mine[0] as usize;
let j = mine[1] as usize;
grid[i][j] = 0;
}
for i in 0..n {
for j in 0..n {
if grid[i][j] == 1 {
if j > 0 {
left[i][j] = left[i][j - 1] + 1;
} else {
left[i][j] = 1;
}
}
}
}
for j in 0..n {
for i in 0..n {
if grid[i][j] == 1 {
if i > 0 {
top[i][j] = top[i - 1][j] + 1;
} else {
top[i][j] = 1;
}
}
}
}
for i in 0..n {
for j in (0..n).rev() {
if grid[i][j] == 1 {
if j + 1 < n {
right[i][j] = right[i][j + 1] + 1;
} else {
right[i][j] = 1;
}
}
}
}
for j in 0..n {
for i in (0..n).rev() {
if grid[i][j] == 1 {
if i + 1 < n {
bottom[i][j] = bottom[i + 1][j] + 1;
} else {
bottom[i][j] = 1;
}
}
}
}
let mut res = 0;
for i in 0..n {
for j in 0..n {
let mut min = n;
min = min.min(left[i][j]);
min = min.min(right[i][j]);
min = min.min(top[i][j]);
min = min.min(bottom[i][j]);
res = res.max(min);
}
}
res as i32
}
}
#[test]
fn test() {
let n = 5;
let mines = vec_vec_i32![[4, 2]];
let res = 2;
assert_eq!(Solution::order_of_largest_plus_sign(n, mines), res);
let n = 2;
let mines = vec_vec_i32![];
let res = 1;
assert_eq!(Solution::order_of_largest_plus_sign(n, mines), res);
let n = 1;
let mines = vec_vec_i32![[0, 0]];
let res = 0;
assert_eq!(Solution::order_of_largest_plus_sign(n, mines), res);
}
// Accepted solution for LeetCode #764: Largest Plus Sign
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #764: Largest Plus Sign
// class Solution {
// public int orderOfLargestPlusSign(int n, int[][] mines) {
// int[][] dp = new int[n][n];
// for (var e : dp) {
// Arrays.fill(e, n);
// }
// for (var e : mines) {
// dp[e[0]][e[1]] = 0;
// }
// for (int i = 0; i < n; ++i) {
// int left = 0, right = 0, up = 0, down = 0;
// for (int j = 0, k = n - 1; j < n; ++j, --k) {
// left = dp[i][j] > 0 ? left + 1 : 0;
// right = dp[i][k] > 0 ? right + 1 : 0;
// up = dp[j][i] > 0 ? up + 1 : 0;
// down = dp[k][i] > 0 ? down + 1 : 0;
// dp[i][j] = Math.min(dp[i][j], left);
// dp[i][k] = Math.min(dp[i][k], right);
// dp[j][i] = Math.min(dp[j][i], up);
// dp[k][i] = Math.min(dp[k][i], down);
// }
// }
// return Arrays.stream(dp).flatMapToInt(Arrays::stream).max().getAsInt();
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.