Sponsored
Sponsored
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.
Time Complexity: O(n^2)
since each cell is visited and calculated twice.
Space Complexity: O(n^2)
due to the four direction matrices.
1using System;
2using System.Linq;
3
4class Solution {
5 public int OrderOfLargestPlusSign(int n, int[][] mines) {
6 int[][] grid = new int[n][];
7 for (int i = 0; i < n; i++) {
8 grid[i] = Enumerable.Repeat(n, n).ToArray();
9 }
10
11 foreach (var mine in mines) {
12 grid[mine[0]][mine[1]] = 0;
13 }
14
for (int r = 0; r < n; r++) {
for (int c = 0, l = 0; c < n; c++) { // left
l = grid[r][c] == 0 ? 0 : l + 1;
grid[r][c] = Math.Min(grid[r][c], l);
}
for (int c = n - 1, r = 0; c >= 0; c--) { // right
r = grid[r][c] == 0 ? 0 : r + 1;
grid[r][c] = Math.Min(grid[r][c], r);
}
}
for (int c = 0; c < n; c++) {
for (int r = 0, u = 0; r < n; r++) { // up
u = grid[r][c] == 0 ? 0 : u + 1;
grid[r][c] = Math.Min(grid[r][c], u);
}
for (int r = n - 1, d = 0; r >= 0; r--) { // down
d = grid[r][c] == 0 ? 0 : d + 1;
grid[r][c] = Math.Min(grid[r][c], d);
}
}
int maxOrder = 0;
for (int r = 0; r < n; r++) {
for (int c = 0; c < n; c++) {
maxOrder = Math.Max(maxOrder, grid[r][c]);
}
}
return maxOrder - 1;
}
static void Main(string[] args) {
Solution sol = new Solution();
int[][] mines = { new int[]{ 4, 2 } };
Console.WriteLine(sol.OrderOfLargestPlusSign(5, mines)); // Output: 2
}
}
Similar to the previous languages, the C# solution uses the same technique to handle directional traversal and minimalizing grid values to reflect possible arm lengths at each point, taking into account any mines placed as zeroes affecting these lengths.
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
.
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.
In Java, the solution leverages sets to rapidly identify zeros (from mines), ensuring each check during computation saves iterating over unnecessary areas without requiring a complete scan. The grid is updated to better reflect ongoing plus sign lengths as iterated.