As Far from Land as Possible LeetCode solution in Python

1
from collections import *
2

3

4
class Solution:
5
def maxDistance(self, grid) -> int:
6
m, n = len(grid), len(grid[0])
7
queue = deque([])
8
for i in range(m):
9
for j in range(n):
10
if grid[i][j] == 1:
11
queue.append((i, j))
12
c = -1
13
while queue:
14
# print(queue)
15
temp = len(queue)
16
for _ in range(temp):
17
(i, j) = queue.popleft()
18
for x, y in ((i - 1, j), (i + 1, j), (i, j - 1), (i, j + 1)):
19
if x < 0 or x >= m or y < 0 or y >= n or grid[x][y] == 1:
20
continue
21
grid[x][y] = 1
22
queue.append((x, y))
23
c += 1
24
return c if c != 0 else -1

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0