-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDay 27
More file actions
26 lines (23 loc) · 921 Bytes
/
Day 27
File metadata and controls
26 lines (23 loc) · 921 Bytes
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
#2257.count-unguarded-cells-in-the-grid:-
class Solution:
def countUnguarded(self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]) -> int:
grid = [[0] * n for _ in range(m)]
for r, c in guards:
grid[r][c] = 1
for r, c in walls:
grid[r][c] = 2
guarded = [[False] * n for _ in range(m)]
directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
for r, c in guards:
for dr, dc in directions:
nr, nc = r + dr, c + dc
while 0 <= nr < m and 0 <= nc < n and grid[nr][nc] != 1 and grid[nr][nc] != 2:
guarded[nr][nc] = True
nr += dr
nc += dc
unguarded = 0
for i in range(m):
for j in range(n):
if grid[i][j] == 0 and not guarded[i][j]:
unguarded += 1
return unguarded