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.This approach involves using dynamic programming to store the maximum possible length of arms of a plus sign centered at each cell, checking in all four directions (left, right, up, down).
We initialize four matrices (left, right, up, down) that store the length of ones segment that ends at each cell in the corresponding direction. We update these matrices while iterating through the grid.
By iterating over the grid, first from top-left to bottom-right, and then from bottom-right to top-left, we can fill these matrices. The maximum order of the plus sign centered at any cell is the minimum value among the four directions at that cell.
The implementation starts by initializing a grid and converting the mines positions to zero. Four matrices (left, right, up, down) store the consecutive 1 segments up to that point from each direction respectively.
We iterate through the grid twice. The first pass, filling left and up, goes top-to-bottom and left-to-right; the second, filling right and down, goes bottom-to-top and right-to-left. Finally, we calculate the minimum of the maximum possible lengths at each point to determine the largest possible plus sign.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2) since each cell is visited and calculated twice.
Space Complexity: O(n^2) due to the four direction matrices.
This approach uses a greedy algorithm with the aid of hash sets to track each encountered zero from the mines. By using sets, we quickly check if any particular cell disrupts a potential plus sign, and can avoid unnecessary calculations.
For every arm of each potential plus sign in the grid, checks are performed to determine continuation down an arm or reset due to zeroes from mines.
This C program uses a 2D boolean array to track where mines are located, to avoid recalculating grid states unnecessarily. It also checks for maximum arm lengths from prior initializations to limit the search scope while calculating potential plus signs.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2) due to the requirement to scan both axes when looking for maximum stretches of ones.
Space Complexity: O(n^2) as the boolean grid requires space for each cell to track presence of mine locations.
| Approach | Complexity |
|---|---|
| Dynamic Programming Approach | Time Complexity: Space Complexity: |
| Greedy Scanning with Set Usage | Time Complexity: Space Complexity: |
Largest Color Value in a Directed Graph - Leetcode 1857 - Python • NeetCodeIO • 22,081 views views
Watch 9 more video solutions →Practice Largest Plus Sign with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor