Sort by

recency

|

277 Discussions

|

  • + 0 comments

    for C# my solution, if there is any improvements on this please do tell

    public static int flippingMatrix(List<List<int>> matrix)
    {
        int sum = 0;
    
        for (int i = 0, len = matrix.Count - 1; i < (matrix.Count / 2); i++, len--)
        {
            for (int j = 0, k = matrix.Count - 1; j < (matrix.Count / 2); j++, k--)
            {
                int[] temp = [matrix[i][j], matrix[i][k], matrix[len][j], matrix[len][k]];
                sum += temp.Max();
            }
        }
        return sum;
    }
    
  • + 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;
        }