Largest Plus Sign
Description
Order 1:
000
010
000
Order 2:
00000
00100
01110
00100
00000
Order 3:
0000000
0001000
0001000
0111110
0001000
0001000
0000000Solutions
DFS
Last updated
Order 1:
000
010
000
Order 2:
00000
00100
01110
00100
00000
Order 3:
0000000
0001000
0001000
0111110
0001000
0001000
0000000Last updated
Input: N = 5, mines = [[4, 2]]
Output: 2
Explanation:
11111
11111
11111
11111
11011
In the above grid, the largest plus sign can only be order 2. One of them is marked in bold.Input: N = 2, mines = []
Output: 1
Explanation:
There is no plus sign of order 2, but there is of order 1.Input: N = 1, mines = [[0, 0]]
Output: 0
Explanation:
There is no plus sign, so return 0.class Solution {
public:
int orderOfLargestPlusSign(int N, vector<vector<int>>& mines) {
vector<vector<int>> grid(N, vector<int>(N, 1));
for(auto &p: mines){
grid[p[0]][p[1]] = 0;
}
auto all_ones = [&](int i, int j, int k){
if(i - k < 0 || i + k >= N || j - k < 0 || j + k >= N)
return false;
return grid[i - k][j] && grid[i + k][j] && grid[i][j - k] && grid[i][j + k];
};
int K = 0;
for(int i = 0; i < N; ++i){
for(int j = 0; j < N; ++j){
if(grid[i][j] == 1){
int k = 1;
while(all_ones(i, j, k))
++k;
K = max(k, K);
}
}
}
return K;
}
};