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/python3importmathimportosimportrandomimportreimportsys## Complete the 'twoPluses' function below.## The function is expected to return an INTEGER.# The function accepts STRING_ARRAY grid as parameter.#deftwoPluses(grid):# Write your code heren,m=len(grid),len(grid[0])up=[[0]*mfor_inrange(n)]down=[[0]*mfor_inrange(n)]left=[[0]*mfor_inrange(n)]right=[[0]*mfor_inrange(n)]foriinrange(n):forjinrange(m):ifgrid[i][j]=='G':up[i][j]=1+(up[i-1][j]ifi>0else0)left[i][j]=1+(left[i][j-1]ifj>0else0)foriinreversed(range(n)):forjinreversed(range(m)):ifgrid[i][j]=='G':down[i][j]=1+(down[i+1][j]ifi<n-1else0)right[i][j]=1+(right[i][j+1]ifj<m-1else0)pluses=[]foriinrange(n):forjinrange(m):ifgrid[i][j]=='G':arm_len=min(up[i][j],down[i][j],left[i][j],right[i][j])-1forlengthinrange(arm_len+1):pluses.append((i,j,length,4*length+1))#(row,col,arm,area)defcells_of_plus(r,c,arm):cells={(r,c)}forkinrange(1,arm+1):cells.add((r+k,c))cells.add((r-k,c))cells.add((r,c+k))cells.add((r,c-k))returncellsmax_product=0foriinrange(len(pluses)):r1,c1,arm1,area1=pluses[i]cells1=cells_of_plus(r1,c1,arm1)forjinrange(i+1,len(pluses)):r2,c2,arm2,area2=pluses[j]cells2=cells_of_plus(r2,c2,arm2)ifcells1.isdisjoint(cells2):product=area1*area2ifproduct>max_product:max_product=productreturnmax_productif__name__=='__main__':fptr=open(os.environ['OUTPUT_PATH'],'w')first_multiple_input=input().rstrip().split()n=int(first_multiple_input[0])m=int(first_multiple_input[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 →