Sort by

recency

|

275 Discussions

|

  • + 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:

  • + 0 comments

    Python 3

    It took me a couple of hours to understand which elements of the matrix need to be summed in order to compute to the max sum, I still do not understand why this pattern applies? How could I have recoginised this symmetry?

    [ ['A', 'B', 'B', 'A'], ['C', 'D', 'D', 'C'], ['C', 'D', 'D', 'C'], ['A', 'B', 'B', 'A'] ]

    `def flippingMatrix(matrix): n = len(matrix) // 2 max_sum = 0

    for row in range(n):
        for col in range(n):
            top_left = matrix[row][col]
            top_right = matrix[row][2*n - 1 - col]
            bottom_left = matrix[2*n - 1 - row][col]
            bottom_right = matrix[2*n - 1 - row][2*n - 1 - col]
    
            max_value =  max(top_left, top_right, bottom_left, bottom_right)
            max_sum += max_value
    
    return max_sum