-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathleet-code-q-67.java
More file actions
44 lines (36 loc) · 1.4 KB
/
leet-code-q-67.java
File metadata and controls
44 lines (36 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
class Solution {
public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
int[][] grid = new int[m][n];
for (int[] guard : guards) grid[guard[0]][guard[1]] = 1;
for (int[] wall : walls) grid[wall[0]][wall[1]] = 2;
for (int[] guard : guards) {
int row = guard[0];
int col = guard[1];
for (int r = row - 1; r >= 0; r--) {
if (grid[r][col] == 1 || grid[r][col] == 2) break;
if (grid[r][col] == 0) grid[r][col] = 3;
}
for (int r = row + 1; r < m; r++) {
if (grid[r][col] == 1 || grid[r][col] == 2) break;
if (grid[r][col] == 0) grid[r][col] = 3;
}
for (int c = col - 1; c >= 0; c--) {
if (grid[row][c] == 1 || grid[row][c] == 2) break;
if (grid[row][c] == 0) grid[row][c] = 3;
}
for (int c = col + 1; c < n; c++) {
if (grid[row][c] == 1 || grid[row][c] == 2) break;
if (grid[row][c] == 0) grid[row][c] = 3;
}
}
int count = 0;
for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
if (grid[i][j] == 0) {
count++;
}
}
}
return count;
}
}