Count Unguarded Cells in the Grid LeetCode solution in Python

1
class Solution:
2
def countUnguarded(
3
self, m: int, n: int, guards: List[List[int]], walls: List[List[int]]
4
) -> int:
5
dp = [[0] * n for _ in range(m)]
6
for x, y in guards + walls:
7
dp[x][y] = 1
8

9
directions = [(0, 1), (1, 0), (-1, 0), (0, -1)]
10

11
for x, y in guards:
12
for dx, dy in directions:
13
curr_x = x
14
curr_y = y
15

16
while (
17
0 <= curr_x + dx < m
18
and 0 <= curr_y + dy < n
19
and dp[curr_x + dx][curr_y + dy] != 1
20
):
21
curr_x += dx
22
curr_y += dy
23
dp[curr_x][curr_y] = 2
24

25
return sum(1 for i in range(m) for j in range(n) if dp[i][j] == 0)

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0