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.
defconnectedCell(matrix):# Write your code hereones=[]foriinrange(len(matrix)):forjinrange(len(matrix[0])):ifmatrix[i][j]==1:ones.append((i,j))l=[]whileones!=[]:one=ones.pop(0)ll=[one]m=1n=0whilen!=m:#count pre expansionm=len(ll)foroneinll:i,j=one[0],one[1]neighbors=[(i-1,j-1),(i-1,j),(i-1,j+1),(i,j-1),(i,j+1),(i+1,j-1),(i+1,j),(i+1,j+1)]forneighborinneighbors:ifneighborinones:ones.remove(neighbor)ll.append(neighbor)#count post expansionn=len(ll)l.append(len(ll))returnmax(l)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Connected Cells in a Grid
You are viewing a single comment's thread. Return to all comments →