Sort by

recency

|

276 Discussions

|

  • + 0 comments

    JAVA BEST SOLUTION

    public static int flippingMatrix(List<List<Integer>> matrix) {
        int len=matrix.size();
        int total=0;
        for(int i=0;i<len/2;i++){
            for(int j=0;j<len/2;j++){
                total+=Math.max(Math.max(matrix.get(i).get(j),matrix.get(i).get(len-j-1)),Math.max((matrix.get(len-i-1).get(len-j-1)),matrix.get(len-1-i).get(j)));
            }
        }
        return total;
        }
    
  • + 0 comments

    Python 3.8+ solution:

    def flippingMatrix(matrix: list[list[int]]) -> int:
        return sum(
            max((row := matrix[i])[j], row[~j], (row := matrix[~i])[j], row[~j])
            for i, j in itertools.product(range(len(matrix) // 2), repeat=2)
        )
    
  • + 0 comments
    def flippingMatrix(matrix):
        return sum(
            max(matrix[i][j], matrix[~i][j], matrix[i][~j], matrix[~i][~j])
            for i in range(len(matrix) >> 1)
            for j in range(len(matrix) >> 1)
        )
    
  • + 0 comments

    Java 15 with streams API:

        public static int flippingMatrix(List<List<Integer>> matrix) {
            int n2 = matrix.size();
            assert n2 % 2 == 0;
            int n = n2 / 2;
            assert 1 <= 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.
            int maxSum = IntStream.range(0, n).map(i ->
                IntStream.range(0, n).map(j ->
                    Stream.of(
                        matrix.get(i).get(j), // top left
                        matrix.get(i).get(n2 - j - 1), // top right
                        matrix.get(n2 - i - 1).get(j), // bottom left
                        matrix.get(n2 - i - 1).get(n2 - j - 1) // bottom right
                    )
                        .max(Comparator.naturalOrder())
                        .get()
                ).sum()
            ).sum();
            return maxSum;
        }
    
  • + 1 comment

    The real difficulty of this problem is realizing that you can change the value of any cell of the first quarter submatrix for any of the other 3 options available without affecting any other value of the first quarter submatrix.

    This can be achieved with a flipping sequence like this:

    The other 2 candidate cells can be moved to the first quarter submatrix using the same sequence as above after using this second sequence: