-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdaily157.java
More file actions
168 lines (136 loc) · 3.76 KB
/
Copy pathdaily157.java
File metadata and controls
168 lines (136 loc) · 3.76 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Solution 1
class Solution {
public int checkGuards(int[][] grid, int[][] seen, int r, int c) {
int total = 0;
for (int i = r + 1; i < grid.length; i++) {
if (seen[i][c] == 1)
continue;
if (grid[i][c] >= 1)
break;
seen[i][c] = 1;
if (grid[i][c] == 0)
total++;
}
for (int i = r - 1; i >= 0; i--) {
if (seen[i][c] == 1)
continue;
if (grid[i][c] >= 1)
break;
seen[i][c] = 1;
if (grid[i][c] == 0)
total++;
}
for (int i = c + 1; i < grid[0].length; i++) {
if (seen[r][i] == 1) {
continue;
}
if (grid[r][i] >= 1)
break;
seen[r][i] = 1;
if (grid[r][i] == 0) {
total++;
}
}
for (int i = c - 1; i >= 0; i--) {
if (seen[r][i] == 1)
continue;
if (grid[r][i] >= 1)
break;
seen[r][i] = 1;
if (grid[r][i] == 0)
total++;
}
return total;
}
public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
/*
start with mxn as resulting total
for each guard sightline
-- total cells until end of grid or encountered wall
takes g^4 time?
OR
have hashmaps for each row and column, !!nvm sarah goated!!
*/
int res = m * n;
int[][] grid = new int[m][n];
for (int[] r: grid) {
Arrays.fill(r, 0);
}
for (int[] g : guards) {
res--;
grid[g[0]][g[1]] = 1;
}
for (int[] w : walls) {
res--;
grid[w[0]][w[1]] = 2;
}
int[][] seen = new int[m][n];
for (int[] s : seen) {
Arrays.fill(s, 0);
}
int r = 0;
int c = 0;
while (r < m && c < n) {
if (grid[r][c] == 1) {
res -= checkGuards(grid, seen, r, c);
}
c++;
if (c == grid[0].length) {
r++;
c = 0;
}
}
return res;
}
}
// Solution 2
class Solution {
public int countUnguarded(int m, int n, int[][] guards, int[][] walls) {
int[][] grid = new int[m][n];
int x, y;
for (int[] guard : guards) {
grid[guard[0]][guard[1]] = 1;
}
for (int[] wall : walls) {
grid[wall[0]][wall[1]] = 1;
}
int count = 0;
for (int[] guard : guards) {
x = guard[0] - 1;
y = guard[1];
while (x >= 0 && grid[x][y] != 1) {
if (grid[x][y] != -1) {
count++;
grid[x][y] = -1;
}
x--;
}
x = guard[0] + 1;
while (x < m && grid[x][y] != 1) {
if (grid[x][y] != -1) {
count++;
grid[x][y] = -1;
}
x++;
}
x = guard[0];
y = guard[1] - 1;
while (y >= 0 && grid[x][y] != 1) {
if (grid[x][y] != -1) {
count++;
grid[x][y] = -1;
}
y--;
}
y = guard[1] + 1;
while (y < n && grid[x][y] != 1) {
if (grid[x][y] != -1) {
count++;
grid[x][y] = -1;
}
y++;
}
}
return m * n - guards.length - walls.length - count;
}
}