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.
1#include <vector>
2#include <algorithm>
3
4using namespace std;
5
6int orderOfLargestPlusSign(int n, vector<vector<int>>& mines) {
7 vector<vector<int>> grid(n, vector<int>(n, n));
8
9 for (auto& mine : mines) {
10 grid[mine[0]][mine[1]] = 0;
11 }
12
13 for (int r = 0; r < n; ++r) {
14 for (int c = 0, l = 0; c < n; ++c) { // left
15 l = (grid[r][c] == 0) ? 0 : l + 1;
16 grid[r][c] = min(grid[r][c], l);
17 }
18 for (int c = n - 1, r = 0; c >= 0; --c) { // right
19 r = (grid[r][c] == 0) ? 0 : r + 1;
20 grid[r][c] = min(grid[r][c], r);
21 }
22 }
23
24 for (int c = 0; c < n; ++c) {
25 for (int r = 0, u = 0; r < n; ++r) { // up
26 u = (grid[r][c] == 0) ? 0 : u + 1;
27 grid[r][c] = min(grid[r][c], u);
28 }
29 for (int r = n - 1, d = 0; r >= 0; --r) { // down
30 d = (grid[r][c] == 0) ? 0 : d + 1;
31 grid[r][c] = min(grid[r][c], d);
32 }
33 }
34
35 int maxOrder = 0;
36 for (int r = 0; r < n; ++r) {
37 for (int c = 0; c < n; ++c) {
38 maxOrder = max(maxOrder, grid[r][c]);
39 }
40 }
41
42 return maxOrder - 1;
43}
44
45int main() {
46 // Example usage:
47 int n = 5;
48 vector<vector<int>> mines = {{4, 2}};
49 int result = orderOfLargestPlusSign(n, mines);
50 cout << result << endl; // Output: 2
51 return 0;
52}
In this implementation, we use the same four-direction method by iterating through the grid. Memory space is optimized by updating the grid in place for a single dimension at a time while determining the minimum consecutive ones from each direction. With each pass, the value stored in each cell represents the maximum arm length possible at that cell for a plus sign.
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.
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.