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.
With aux class so I can make a tupple without resorting to using stings and split():
classOneSpot{intx;inty;publicOneSpot(intx,inty){this.x=x;this.y=y;}@Overridepublicbooleanequals(Objecto){OneSpots=(OneSpot)o;return(s.x==x&&s.y==y);}@OverridepublicinthashCode(){returny*100+x;}}classResult{/* * Complete the 'connectedCell' function below. * * The function is expected to return an INTEGER. * The function accepts 2D_INTEGER_ARRAY matrix as parameter. */publicstaticvoidgrow(OneSpotspot,Set<OneSpot>ones,intxmax,intymax,Set<OneSpot>seen,Set<OneSpot>ourgang){for(intdx=-1;dx<2;dx++){for(intdy=-1;dy<2;dy++){intnx=spot.x+dx;intny=spot.y+dy;OneSpotnspot=newOneSpot(nx,ny);if(ones.contains(nspot)&&!seen.contains(nspot)){ourgang.add(nspot);seen.add(nspot);grow(nspot,ones,xmax,ymax,seen,ourgang);}}}}publicstaticintconnectedCell(List<List<Integer>>matrix){// Write your code hereSet<OneSpot>ones=newHashSet<>();intymax=matrix.size()-1;intxmax=matrix.get(0).size()-1;for(inty=0;y<=ymax;y++){for(intx=0;x<=xmax;x++){if(matrix.get(y).get(x)==1){ones.add(newOneSpot(x,y));}}}intsize=0;Set<OneSpot>seen=newHashSet<>();for(OneSpotspot:ones){Set<OneSpot>ourgang=newHashSet<>();if(seen.contains(spot)){continue;}ourgang.add(spot);seen.add(spot);grow(spot,ones,xmax,ymax,seen,ourgang);if(ourgang.size()>size){size=ourgang.size();}}returnsize;}}
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 →
With aux class so I can make a tupple without resorting to using stings and split():