Number of Paths with Max Score

You are given a square board of characters. You can move on the board starting at the bottom right square marked with the character 'S'.

You need to reach the top left square marked with the character 'E'. The rest of the squares are labeled either with a numeric character 1, 2, ..., 9 or with an obstacle 'X'. In one move you can go up, left or up-left (diagonally) only if there is no obstacle there.

Return a list of two integers: the first integer is the maximum sum of numeric characters you can collect, and the second is the number of such paths that you can take to get that maximum sum, taken modulo 10^9 + 7.

In case there is no path, return [0, 0].

 

Example 1:

Input: board = ["E23","2X2","12S"]
Output: [7,1]

Example 2:

Input: board = ["E12","1X1","21S"]
Output: [4,2]

Example 3:

Input: board = ["E11","XXX","11S"]
Output: [0,0]

 

Constraints:


Solution:

class Solution {
    int mod = (int) 1e9 + 7;
    
    static class Pair {
        int score;
        int path;
        
        public Pair(int x, int y) {
            score = x;
            path = y;
        }
        
        public String toString() {
            return "<" + score + ", " + path + ">";
        }
    }
    
    public int[] pathsWithMaxScore(List<String> board) {
        int m = board.size(), n = board.get(0).length();
        Pair[][] dp = new Pair[m][n];
        for (int i = 0; i < m; i ++) {
            for (int j = 0; j < n; j ++) {
                char c = board.get(i).charAt(j);
                if (i == 0 && j == 0) {
                    dp[i][j] = new Pair(0, 1);
                } else if (i == 0) {
                    if (c != 'X') {
                        int val = c == 'S' ? 0 : c - '0';
                        Pair p = new Pair(dp[i][j - 1].score + val, dp[i][j - 1].path);
                        dp[i][j] = p;
                    } else {
                        dp[i][j] = new Pair(0, 0);
                    }
                } else if (j == 0) {
                    if (c != 'X') {
                        int val = c == 'S' ? 0 : c - '0';
                        Pair p = new Pair(dp[i - 1][j].score + val, dp[i - 1][j].path);
                        dp[i][j] = p;
                    } else {
                        dp[i][j] = new Pair(0, 0);
                    }             
                } else {
                    if (c != 'X') {
                        int val = c == 'S' ? 0 : c - '0';
                        int maxVal = 0;
                        int maxCount = 0;
                        if (dp[i - 1][j].score > maxVal) {
                            maxVal = dp[i - 1][j].score;
                            maxCount = dp[i - 1][j].path;
                        }
                        if (dp[i][j - 1].score > maxVal) {
                            maxVal = dp[i][j - 1].score;
                            maxCount = dp[i][j - 1].path;
                        } else if (dp[i][j - 1].score == maxVal) {
                            maxCount = (maxCount + dp[i][j - 1].path) % mod;
                        }
                        if (dp[i - 1][j - 1].score > maxVal) {
                            maxVal = dp[i - 1][j - 1].score;
                            maxCount = dp[i - 1][j - 1].path;
                        } else if (dp[i - 1][j - 1].score == maxVal) {
                            maxCount = (maxCount + dp[i - 1][j - 1].path) % mod;
                        }                  
                        dp[i][j] = new Pair(maxVal + val, maxCount % mod);
                    } else {
                        dp[i][j] = new Pair(0, 0);
                    }
                }
            }
        }
        // for (int i = 0; i < m; i ++) {
        //     for (int j = 0; j < n; j ++) {
        //         System.out.print(dp[i][j] + ", ");
        //     }
        //     System.out.println();
        // }
        if (dp[m - 1][n - 1].path > 0) {
            return new int[]{dp[m - 1][n - 1].score, dp[m - 1][n - 1].path};
        } else {
            return new int[]{0, 0};
        }
    }
}