-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathMax Area of Island.py
56 lines (51 loc) · 1.72 KB
/
Max Area of Island.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# Max Area of Island
# https://leetcode.com/problems/max-area-of-island/
class Solution(object):
def __init__(self):
self.maxSize = 0
def isValid(self, row, col, grid):
if (row >= 0 and row < len(grid)):
if (col >= 0 and col < len(grid[0])):
if (grid[row][col] == 1):
return True
return False
def nextValidMoves(self, row, col, grid):
moves = []
# up
if (self.isValid(row-1, col, grid)):
moves.append([row-1, col])
# down
if (self.isValid(row+1, col, grid)):
moves.append([row+1, col])
# left
if (self.isValid(row, col-1, grid)):
moves.append([row, col-1])
# right
if (self.isValid(row, col+1, grid)):
moves.append([row, col+1])
return moves
def findIsland(self, row, col, grid, size):
if (grid[row][col]==0):
return
print 'row',row, 'col',col
# Mark current as visited
grid[row][col] = 0
# increment the size
size[0] = size[0]+1
# get the next moves
moves = self.nextValidMoves(row, col, grid)
for move in moves:
self.findIsland(move[0], move[1], grid, size)
def maxAreaOfIsland(self, grid):
for row in range(len(grid)):
for col in range(len(grid[0])):
if (grid[row][col] == 1):
size = [0]
self.findIsland(row, col, grid, size)
self.maxSize = max(self.maxSize, size[0])
print size
return self.maxSize
"""
:type grid: List[List[int]]
:rtype: int
"""