Max Area of Island

[[0,0,1,0,0,0,0,1,0,0,0,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,1,1,0,1,0,0,0,0,0,0,0,0],
[0,1,0,0,1,1,0,0,1,0,1,0,0],
[0,1,0,0,1,1,0,0,1,1,1,0,0],
[0,0,0,0,0,0,0,0,0,0,1,0,0],
[0,0,0,0,0,0,0,1,1,1,0,0,0],
[0,0,0,0,0,0,0,1,1,0,0,0,0]]
[[0,0,0,0,0,0,0,0]]
def maxAreaOfIsland(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""

area = 0
x = len(grid)
y = len(grid[0])

def addNeighbor(myStack, i, j):
if i-1 >= 0 and grid[i-1][j] == 1:
grid[i-1][j] = 0
myStack.append([i-1, j])
if i+1 < x and grid[i+1][j] == 1:
grid[i+1][j] = 0
myStack.append([i+1, j])
if j - 1 >= 0 and grid[i][j-1] == 1:
grid[i][j-1] = 0
myStack.append([i, j-1])
if j + 1 < y and grid[i][j+1] == 1:
grid[i][j+1] = 0
myStack.append([i, j+1])

for i in range(len(grid)):
for j in range(len(grid[0])):
if grid[i][j] == 1:
local_area = 0
grid[i][j] = 0
stack = []
stack.append([i,j])
while stack:
newPair = stack.pop()
grid[newPair[0]][newPair[1]] = 0
addNeighbor(stack, newPair[0], newPair[1])
local_area += 1

area = max(area, local_area)
return area

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store