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.
#!/bin/python3importmathimportosimportrandomimportreimportsysfromitertoolsimportcombinations# Complete the 'twoPluses' function below.# The function is expected to return an INTEGER.# The function accepts STRING_ARRAY grid as parameter.deftwoPluses(grid):h,w=len(grid),len(grid[0])pluses=[]defis_good(r,c):return0<=r<hand0<=c<wandgrid[r][c]=='G'# Generate all valid plusesforrinrange(h):forcinrange(w):ifnotis_good(r,c):continuesize=0occupied={(r,c)}pluses.append((1,occupied.copy()))#AddsinglecellpluswhileTrue:size+=1cells={(r+i,c)foriinrange(-size,size+1)}|{(r,c+i)foriinrange(-size,size+1)}ifall(is_good(x,y)forx,yincells):pluses.append((len(cells),cells.copy()))else:break# Try all combinations of 2 pluses and find max area product of non-overlapping onesmax_product=0for(area1,cells1),(area2,cells2)incombinations(pluses,2):ifcells1.isdisjoint(cells2):max_product=max(max_product,area1*area2)returnmax_productifmax_productelse1#Atleastoneplusisguaranteedif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')nm=input().split()n=int(nm[0])m=int(nm[1])grid=[]for_inrange(n):grid_item=input()grid.append(grid_item)result=twoPluses(grid)fptr.write(str(result)+'\n')fptr.close()
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 →