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

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to sign a URLRequest and download a file from S3 in iOS.

Let’s learn how to to build a chat application with Redis, WebSocket and Go

I Altered My Redshift Schema’s Default Privileges. Why can’t my users query my new tables?

Snojan Analysis

Rails Code Challenge

Loving First Year of Loves Cloud

Writing SQL queries in Python with SQLite

Cinematic Stealth Project — Game Manager #12

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
Newbie Developer

Newbie Developer

More from Medium

Applications of Computational Geometry

CS373 Spring 2022: Scarlett Shires: Final Entry

CS373 Spring 2022: An Vi Nguyen

Seeding stem cells into the human brain may cure Parkinson’s disease