8000 * Done with "1992. Find All Groups of Farmland". · garciparedes/leetcode@f2ec28c · GitHub
[go: up one dir, main page]

Skip to content

Commit f2ec28c

Browse files
committed
* Done with "1992. Find All Groups of Farmland".
1 parent a00091a commit f2ec28c

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
class Solution:
2+
def findFarmland(self, land: List[List[int]]) -> List[List[int]]:
3+
4+
ans = list()
5+
for i in range(len(land)):
6+
for j in range(len(land[0])):
7+
found = self._explore(land, i, j)
8+
if found is None:
9+
continue
10+
ans.append(found)
11+
return ans
12+
13+
def _explore(self, land: List[List[int]], i: int, j: int) -> Optional[List[int]]:
14+
if i < 0 or i >= len(land) or j < 0 or j >= len(land[0]) or land[i][j] != 1:
15+
return None
16+
17+
land[i][j] = 2
18+
19+
ans = [i, j, i ,j]
20+
21+
tmp = self._explore(land, i - 1, j)
22+
self._update_ans(ans, tmp)
23+
24+
tmp = self._explore(land, i + 1, j)
25+
self._update_ans(ans, tmp)
26+
27+
tmp = self._explore(land, i, j - 1)
28+
self._update_ans(ans, tmp)
29+
30+
tmp = self._explore(land, i, j + 1)
31+
self._update_ans(ans, tmp)
32+
33+
return ans
34+
35+
@staticmethod
36+
def _update_ans(ans: List[int], tmp: Optional[List[int]]) -> None:
37+
if tmp is None:
38+
return
39+
40+
if tmp[0] < ans[0]:
41+
ans[0] = tmp[0]
42+
if tmp[1] < ans[1]:
43+
ans[1] = tmp[1]
44+
if tmp[2] > ans[2]:
45+
ans[2] = tmp[2]
46+
if tmp[3] > ans[3]:
47+
ans[3] = tmp[3]
48+
49+

0 commit comments

Comments
 (0)
0