Skip to content

Commit 1c9a52f

Browse files
authored
Create find-all-groups-of-farmland.py
1 parent db99a19 commit 1c9a52f

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

Python/find-all-groups-of-farmland.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Time: O(m * n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def findFarmland(self, land):
6+
"""
7+
:type land: List[List[int]]
8+
:rtype: List[List[int]]
9+
"""
10+
result = []
11+
for i in xrange(len(land)):
12+
for j in xrange(len(land[0])):
13+
if land[i][j] != 1:
14+
continue
15+
ni, nj = i, j
16+
while ni+1 < len(land) and land[ni+1][j] == 1:
17+
ni += 1
18+
while nj+1 < len(land[0]) and land[i][nj+1] == 1:
19+
nj += 1
20+
for r in xrange(i, ni+1):
21+
for c in xrange(j, nj+1):
22+
land[r][c] = -1
23+
result.append([i, j, ni, nj])
24+
return result

0 commit comments

Comments
 (0)