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.
defmaxRegion(grid):defdfs(grid,row,col):grid[row][col]=0visited.add((row,col))cur=1# 8 directionssteps=[(row-1,col-1),(row-1,col),(row-1,col+1),(row,col-1),(row,col+1),(row+1,col-1),(row+1,col),(row+1,col+1)]# Check if over edge and visitedforr,cinsteps:if0<=r<len(grid)and0<=c<len(grid[0])andgrid[r][c]==1andgrid[r][c]notinvisited:cur+=dfs(grid,r,c)returncurans=0visited=set()foriinrange(len(grid)):forjinrange(len(grid[0])):ifgrid[i][j]==1:res=dfs(grid,i,j)ans=max(ans,res)returnans
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 Python3 solution