Sort by

recency

|

283 Discussions

|

  • + 0 comments

    I just ran into this problem on a test that I had 24 min to solve. I didn't solve it in time. My brain spun to even see the pattern needed beyond getting the max value. So I went and learned the trick online... https://youtu.be/4rin1enhuQQ?si=By2NI2du9ZO6qtf7

  • + 0 comments

    https://github.com/SWT92/hackerrank_flipmatrix

    My answer seems different from the originator, anyone got 483 the following matrix?

    matrix = [[112, 42, 83, 119], [56, 125, 56, 49],[15, 78, 101, 43], [62, 98, 114, 108]]

  • + 0 comments

    this problem deep fried my brain

  • + 1 comment

    Symmetry: We exploit the symmetry of the 2Nx2N matrix to reduce calculations. Maximum value: For each position inside N x N, we find the maximum among the number and its three symmetric counterparts. Summation: The sum of these maximum values gives us the desired result.

        public static int flippingMatrix(List<List<Integer>> matrix) {
            int n = matrix.get(0).size()/2;
            int result = 0; 
            for(int i = 0; i<n; i++) {
                for(int j = 0; j<n; j++){
                    result += Math.max(matrix.get(i).get(j), Math.max(matrix.get(2*n-1-i).get(j),
                    Math.max(matrix.get(2*n-1-i).get(2*n-1-j), matrix.get(i).get(2*n-1-j))));
                }
            }
            return result;
        }
    
  • + 2 comments
    def update_location(i,j,location):
        if i == len(location) - 1 and j == len(location) - 1:
        # if i and j == last row and column in location then do nothing
            pass
    
        elif j == len(location) - 1:  # if j == last index, update next row index 0
    
            if location[i + 1][0][1] == matrix[i][j + 1]:  # [i+1][0] == next row index 0
                location[i + 1][0][0] = 1
            elif location[i + 1][0][1] == matrix[i][-1 - j]:
                location[i + 1][0][0] = 2
            elif location[i + 1][0][1] == matrix[-1 - i][j + 1]:
                location[i + 1][0][0] = 3
            elif location[i + 1][0][1] == matrix[-1 - i][-1 - j]:
                location[i + 1][0][0] = 4
        else:  # update next j+1
            print('******************')
            print('max = '+ str(location[i][j + 1][1]))
            print(matrix[i][j + 1],matrix[i][-1 - (j+1)],matrix[-1 - i][j + 1],matrix[-1 - i][-1 - (j+1)])
            if location[i][j + 1][1] == matrix[i][j + 1]:
                print('update to 1')
                # if j exceed len(location), go to next i and start j = 0 again
                location[i][j + 1][0] = 1
            elif location[i][j + 1][1] == matrix[i][-1 - (j+1)]: #need to use -(j+1)
                print('update to 2')
                location[i][j + 1][0] = 2
            elif location[i][j + 1][1] == matrix[-1 - i][j + 1]:
                print('choose to 3')
                location[i][j + 1][0] = 3
    
            elif location[i][j + 1][1] == matrix[-1 - i][-1 - (j+1)]:
                print('update to 4')
                location[i][j + 1][0] = 4
        return location
    
        #6x6 wrong at 1,1 and 2,1 26 should be 29 and 20 ->23
        #step24 num 20 doing Q4 then update wrong location
        # before step 24 doing Q4
        #step25 num 26 doing Q4 then update wrong location
        # before step 25 doing Q4
    
        #เข้าเงื่อนไข else แต่ max ไม่ตรงกับ ค่าใน matrix เลยดักไม่ได้
    
    # if max in q1 -> no move
    # if max in q2 -> cur box down, max box down, max box left , max box up
    # if max in q3 -> cur box right, max box right, max box up, max box left
    # if max in q4 -> cur box right, max box up, max box left