medium +25 pts

Largest Plus Sign

Find the largest plus sign of 1s in a binary grid with mines.

In an n x n binary grid, each cell is either 1 or 0. A plus sign of order k (k >= 1) is centered at some cell and consists of four arms of equal length extending up, down, left, and right. More precisely, a plus sign of order k on cell (r, c) exists if and only if all cells (r, c), (r - d, c), (r + d, c), (r, c - d), (r, c + d) are all 1 for every d from 0 to k-1. In other words, the arms extend k-1 cells away from the center, so the total length of each arm is k cells (including the center). You are given an integer n and a list of mine coordinates where the cell is 0; all other cells are 1. Implement the function `largest_plus_sign(n: int, mines: list[tuple[int, int]]) -> int` that returns the order of the largest plus sign that can be found in the grid. If no plus sign of order 1 exists (i.e., no cell is 1), return 0. Note: Coordinates are zero-indexed (0 <= row, col < n). Mines are given as a list of (row, col) tuples, and they are guaranteed to be distinct and within bounds.

Constraints

- 1 <= n <= 500 - 0 <= len(mines) <= n * n - Each mine is a tuple (r, c) with 0 <= r, c < n - Time complexity should be O(n^2), space O(n^2) acceptable.

Example

>>> largest_plus_sign(5, [(4, 2)])
2
>>> largest_plus_sign(1, [])
1
>>> largest_plus_sign(1, [(0, 0)])
0
>>> largest_plus_sign(2, [])
1
25 points ~30 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

For each cell, consider the maximum arm length in each direction; the plus size is limited by the smallest of the four.
You can precompute left/right/up/down continuous 1 counts with four passes over the grid.
The final answer is the maximum over all cells of the minimum of the four arm lengths.
Handle the case where the entire grid is 0 by returning 0.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.