-
Notifications
You must be signed in to change notification settings - Fork 119
/
Copy pathFind All Groups of Farmland.py
37 lines (27 loc) · 1.05 KB
/
Find All Groups of Farmland.py
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
# Runtime: 1416 ms (Top 90.80%) | Memory: 33.1 MB (Top 49.64%)
class Solution:
def findFarmland(self, land: List[List[int]]) -> List[List[int]]:
n = len(land)
m = len(land[0])
groups = []
visited = set()
for y in range(n):
for x in range(m):
if land[y][x] == 0:
continue
if (y, x) in visited:
continue
q = collections.deque()
q.append((y, x))
visited.add((y, x))
while q:
cy, cx = q.popleft()
for dy, dx in ((0, 1), (1, 0)):
if (cy + dy, cx + dx) in visited:
continue
if 0 <= cy + dy < n and 0 <= cx + dx < m:
if land[cy + dy][cx + dx] == 1:
q.append((cy + dy, cx + dx))
visited.add((cy + dy, cx + dx))
groups.append([y, x, cy, cx])
return groups