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.
publicstaticintflippingMatrix(List<List<Integer>>matrix){intn2=matrix.size();assertn2%2==0;intn=n2/2;assert1<=n&&n<=128;// similar to a rubiks cube, through a combination of col & row reversals,// it is possible to swap any value with another symmetrical across the // middle vertical and horizontal axes. Because of this, we can simply choose// the maximum value for the 4 possible positions mirrored across the 4 quadrants.intmaxSum=IntStream.range(0,n).map(i->IntStream.range(0,n).map(j->Stream.of(matrix.get(i).get(j),// top leftmatrix.get(i).get(n2-j-1),// top rightmatrix.get(n2-i-1).get(j),// bottom leftmatrix.get(n2-i-1).get(n2-j-1)// bottom right).max(Comparator.naturalOrder()).get()).sum()).sum();returnmaxSum;}
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 →
Java 15 with streams API: