We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
defexploreSize(grid,r,c,visited):rowInBound=r>=0andr<len(grid)colInBound=c>=0andc<len(grid[0])pos=str(r)+','+str(c)ifnotrowInBoundornotcolInBound:return0ifposinvisited:return0ifgrid[r][c]==0:return0visited.add(pos)size=1size+=exploreSize(grid,r-1,c,visited)size+=exploreSize(grid,r+1,c,visited)size+=exploreSize(grid,r,c-1,visited)size+=exploreSize(grid,r,c+1,visited)size+=exploreSize(grid,r-1,c-1,visited)size+=exploreSize(grid,r+1,c+1,visited)size+=exploreSize(grid,r-1,c+1,visited)size+=exploreSize(grid,r+1,c-1,visited)returnsizedefmaxRegion(grid):# Write your code herevisited=set()maxSize=-1forrinrange(len(grid)):forcinrange(len(grid[0])):size=exploreSize(grid,r,c,visited)ifsize>maxSize:maxSize=sizereturnmaxSize
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
DFS: Connected Cell in a Grid
You are viewing a single comment's thread. Return to all comments →
Here is my Python 3 solution