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.
Decided to get the coordinates of all possible crosses, then get the area for each combination of 2 that don't have overlapping coordinates, and finally return the maximum of those areas.
Got stuck for a while until I remembered I was storing a container within another container so changing the position also cleared it in the list. Worked perfectly after importing 'copy.deepcopy()'.
Python 3
importitertoolsfromcopyimportdeepcopydeftwoPluses(grid):count="".join(grid).count("G")#If there is one or less 'G', then the product will be 0ifcount<2:return0#Need at least a 3x3 grid along with six 'G's to have a cross as an optionifn==2orm==2orcount<6:return1pos_list=[]#Get the maximum of every possible 'G' cross in the gridpos=set()foryinrange(n):forxinrange(m):ifgrid[y][x]=="G":size=1pos.add((x,y))pos_list.append(deepcopy(pos))whileall([x-size>=0,x+size<m,y-size>=0,y+size<n]):current_pos=[(x-size,y),(x+size,y),(x,y-size),(x,y+size)]if"B"inmap(lambdax:grid[x[1]][x[0]],current_pos):breaksize+=1pos.update(current_pos)pos_list.append(deepcopy(pos))pos.clear()#Filter out the combinations that have overlapping squarescombos=filter(lambdax:notbool(x[0]&x[1]),itertools.combinations(pos_list,2))#Get the area of each remaining combinationareas=map(lambdax:len(x[0])*len(x[1]),combos)#Return the maximum of the areasreturnmax(areas)
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Ema's Supercomputer
You are viewing a single comment's thread. Return to all comments →
Decided to get the coordinates of all possible crosses, then get the area for each combination of 2 that don't have overlapping coordinates, and finally return the maximum of those areas.
Got stuck for a while until I remembered I was storing a container within another container so changing the position also cleared it in the list. Worked perfectly after importing 'copy.deepcopy()'.
Python 3