15ms Java solution with matrix


  • 0
    public int pathSum(int[] nums) {
            int[][] matrix = new int[6][17];
            for(int[] line : matrix) {
                Arrays.fill(line,Integer.MAX_VALUE);
            }
            for(int num : nums) {
                int i = num / 100;
                num %= 100;
                int j = num / 10;
                num %= 10;
                matrix[i][j] = num;
            }  
            
            int res = 0;
            for(int i = 4; i >= 1; i--) {
                for(int j = 1; j <= 8; j++) {
                    if( matrix[i][j] != Integer.MAX_VALUE && matrix[i + 1][2 * j] == Integer.MAX_VALUE && 
                       matrix[i + 1][2 * j - 1] == Integer.MAX_VALUE) {
                        int x = i, y = j;
                        while(x >= 1) {
                            res += matrix[x][y];
                            x--;
                            y = (y + 1) / 2;
                        }
                    }
                }
            }
            return res;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.