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.
defflippingMatrix(matrix:list[list[int]])->int:"""Sinceanynumberofcolumn/rowflipsarepossible,Themaximumvaluesmustcomefromrotationsaroundthecenter.Also,sincetheproblemdoesn'trequiretheresultantmatrix,wecansimplydeterminethelargestvaluesforeachpositionthenreturntheirsum.q=4n=q/2m=[[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16],]Topleftquadmustconsistofmaxforeachrotation:0,0:max([1,4,13,16])0,1:max([2,3,14,15])1,0:max([5,8,9,12])1,1:max([6,7,10,11])Generalized,thesecanberepresentedasfollows:r,c:max([m[r][c],m[r][q-1-c],m[q-1-r][c],m[q-1-r][q-1-c]])findthemaxvalueforeachr,csetreturnthesumofmaxvalues"""q=len(matrix)n=q//2m=matrix# get quad value arraysquad_values=[[max([m[r][c],m[r][q-1-c],m[q-1-r][c],m[q-1-r][q-1-c]])forcinrange(n)]forrinrange(n)]print(quad_values)returnsum([sum([cforcinr])forrinquad_values])
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Flipping the Matrix
You are viewing a single comment's thread. Return to all comments →
Solution with explanation in Python 3: