Problem

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].

Examples

Example 1

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

Example 2

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

Example 3

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

Constraints

  • 2 <= board.length == board[i].length <= 100

Solution

Method 1 – Dynamic Programming

Intuition

Use dynamic programming to track the maximum score and the number of ways to reach each cell, moving only up, left, or diagonally up-left, and avoiding obstacles.

Approach

  1. Initialize a DP table where each cell stores the maximum score and number of ways to reach it.
  2. Start from the bottom-right (‘S’) and move towards the top-left (‘E’), considering only valid moves.
  3. For each cell, update its DP value based on the three possible directions.
  4. If a cell is an obstacle, skip it.
  5. At the end, return the result for the top-left cell (‘E’).

Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
public:
    vector<int> pathsWithMaxScore(vector<string>& board) {
        int n = board.size(), MOD = 1e9 + 7;
        vector<vector<pair<int,int>>> dp(n, vector<pair<int,int>>(n, {0,0}));
        dp[n-1][n-1] = {0,1};
        for (int i = n-1; i >= 0; --i) {
            for (int j = n-1; j >= 0; --j) {
                if (board[i][j] == 'X' || (i == n-1 && j == n-1)) continue;
                int maxScore = -1, ways = 0;
                for (auto [di, dj] : vector<pair<int,int>>{{i+1,j},{i,j+1},{i+1,j+1}}) {
                    if (di < n && dj < n && dp[di][dj].second > 0) {
                        int score = dp[di][dj].first;
                        if (score > maxScore) {
                            maxScore = score;
                            ways = dp[di][dj].second;
                        } else if (score == maxScore) {
                            ways = (ways + dp[di][dj].second) % MOD;
                        }
                    }
                }
                if (maxScore == -1) continue;
                int val = (board[i][j] == 'E') ? 0 : board[i][j] - '0';
                dp[i][j] = {maxScore + val, ways};
            }
        }
        auto [score, ways] = dp[0][0];
        return ways == 0 ? vector<int>{0,0} : vector<int>{score,ways};
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
func pathsWithMaxScore(board []string) []int {
    n, MOD := len(board), int(1e9+7)
    dp := make([][]struct{score, ways int}, n)
    for i := range dp {
        dp[i] = make([]struct{score, ways int}, n)
    }
    dp[n-1][n-1].ways = 1
    for i := n-1; i >= 0; i-- {
        for j := n-1; j >= 0; j-- {
            if board[i][j] == 'X' || (i == n-1 && j == n-1) { continue }
            maxScore, ways := -1, 0
            for _, d := range [][2]int{{i+1,j},{i,j+1},{i+1,j+1}} {
                di, dj := d[0], d[1]
                if di < n && dj < n && dp[di][dj].ways > 0 {
                    score := dp[di][dj].score
                    if score > maxScore {
                        maxScore = score
                        ways = dp[di][dj].ways
                    } else if score == maxScore {
                        ways = (ways + dp[di][dj].ways) % MOD
                    }
                }
            }
            if maxScore == -1 { continue }
            val := 0
            if board[i][j] != 'E' { val = int(board[i][j] - '0') }
            dp[i][j].score = maxScore + val
            dp[i][j].ways = ways
        }
    }
    if dp[0][0].ways == 0 { return []int{0,0} }
    return []int{dp[0][0].score, dp[0][0].ways}
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
    public int[] pathsWithMaxScore(List<String> board) {
        int n = board.size(), MOD = 1000000007;
        int[][] score = new int[n][n];
        int[][] ways = new int[n][n];
        ways[n-1][n-1] = 1;
        for (int i = n-1; i >= 0; --i) {
            for (int j = n-1; j >= 0; --j) {
                if (board.get(i).charAt(j) == 'X' || (i == n-1 && j == n-1)) continue;
                int maxScore = -1, cnt = 0;
                for (int[] d : new int[][]{{i+1,j},{i,j+1},{i+1,j+1}}) {
                    int di = d[0], dj = d[1];
                    if (di < n && dj < n && ways[di][dj] > 0) {
                        int s = score[di][dj];
                        if (s > maxScore) {
                            maxScore = s;
                            cnt = ways[di][dj];
                        } else if (s == maxScore) {
                            cnt = (cnt + ways[di][dj]) % MOD;
                        }
                    }
                }
                if (maxScore == -1) continue;
                int val = board.get(i).charAt(j) == 'E' ? 0 : board.get(i).charAt(j) - '0';
                score[i][j] = maxScore + val;
                ways[i][j] = cnt;
            }
        }
        return ways[0][0] == 0 ? new int[]{0,0} : new int[]{score[0][0], ways[0][0]};
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
    fun pathsWithMaxScore(board: List<String>): IntArray {
        val n = board.size
        val MOD = 1_000_000_007
        val score = Array(n) { IntArray(n) }
        val ways = Array(n) { IntArray(n) }
        ways[n-1][n-1] = 1
        for (i in n-1 downTo 0) {
            for (j in n-1 downTo 0) {
                if (board[i][j] == 'X' || (i == n-1 && j == n-1)) continue
                var maxScore = -1
                var cnt = 0
                for ((di, dj) in listOf(i+1 to j, i to j+1, i+1 to j+1)) {
                    if (di < n && dj < n && ways[di][dj] > 0) {
                        val s = score[di][dj]
                        if (s > maxScore) {
                            maxScore = s
                            cnt = ways[di][dj]
                        } else if (s == maxScore) {
                            cnt = (cnt + ways[di][dj]) % MOD
                        }
                    }
                }
                if (maxScore == -1) continue
                val valCell = if (board[i][j] == 'E') 0 else board[i][j] - '0'
                score[i][j] = maxScore + valCell
                ways[i][j] = cnt
            }
        }
        return if (ways[0][0] == 0) intArrayOf(0,0) else intArrayOf(score[0][0], ways[0][0])
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution:
    def pathsWithMaxScore(self, board: list[str]) -> list[int]:
        n, MOD = len(board), 10**9+7
        score = [[0]*n for _ in range(n)]
        ways = [[0]*n for _ in range(n)]
        ways[n-1][n-1] = 1
        for i in range(n-1, -1, -1):
            for j in range(n-1, -1, -1):
                if board[i][j] == 'X' or (i == n-1 and j == n-1): continue
                maxScore, cnt = -1, 0
                for di, dj in [(i+1,j),(i,j+1),(i+1,j+1)]:
                    if di < n and dj < n and ways[di][dj] > 0:
                        s = score[di][dj]
                        if s > maxScore:
                            maxScore = s
                            cnt = ways[di][dj]
                        elif s == maxScore:
                            cnt = (cnt + ways[di][dj]) % MOD
                if maxScore == -1: continue
                val = 0 if board[i][j] == 'E' else int(board[i][j])
                score[i][j] = maxScore + val
                ways[i][j] = cnt
        return [0,0] if ways[0][0] == 0 else [score[0][0], ways[0][0]]
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
impl Solution {
    pub fn paths_with_max_score(board: Vec<String>) -> Vec<i32> {
        let n = board.len();
        let mod_val = 1_000_000_007;
        let mut score = vec![vec![0; n]; n];
        let mut ways = vec![vec![0; n]; n];
        ways[n-1][n-1] = 1;
        for i in (0..n).rev() {
            for j in (0..n).rev() {
                if board[i].as_bytes()[j] == b'X' || (i == n-1 && j == n-1) { continue; }
                let mut max_score = -1;
                let mut cnt = 0;
                for (di, dj) in [(i+1,j),(i,j+1),(i+1,j+1)] {
                    if di < n && dj < n && ways[di][dj] > 0 {
                        let s = score[di][dj];
                        if s > max_score {
                            max_score = s;
                            cnt = ways[di][dj];
                        } else if s == max_score {
                            cnt = (cnt + ways[di][dj]) % mod_val;
                        }
                    }
                }
                if max_score == -1 { continue; }
                let val = if board[i].as_bytes()[j] == b'E' { 0 } else { (board[i].as_bytes()[j] - b'0') as i32 };
                score[i][j] = max_score + val;
                ways[i][j] = cnt;
            }
        }
        if ways[0][0] == 0 { vec![0,0] } else { vec![score[0][0], ways[0][0]] }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Solution {
    pathsWithMaxScore(board: string[]): number[] {
        const n = board.length, MOD = 1e9 + 7;
        const score: number[][] = Array.from({length: n}, () => Array(n).fill(0));
        const ways: number[][] = Array.from({length: n}, () => Array(n).fill(0));
        ways[n-1][n-1] = 1;
        for (let i = n-1; i >= 0; --i) {
            for (let j = n-1; j >= 0; --j) {
                if (board[i][j] === 'X' || (i === n-1 && j === n-1)) continue;
                let maxScore = -1, cnt = 0;
                for (const [di, dj] of [[i+1,j],[i,j+1],[i+1,j+1]]) {
                    if (di < n && dj < n && ways[di][dj] > 0) {
                        const s = score[di][dj];
                        if (s > maxScore) {
                            maxScore = s;
                            cnt = ways[di][dj];
                        } else if (s === maxScore) {
                            cnt = (cnt + ways[di][dj]) % MOD;
                        }
                    }
                }
                if (maxScore === -1) continue;
                const val = board[i][j] === 'E' ? 0 : Number(board[i][j]);
                score[i][j] = maxScore + val;
                ways[i][j] = cnt;
            }
        }
        return ways[0][0] === 0 ? [0,0] : [score[0][0], ways[0][0]];
    }
}

Complexity

  • ⏰ Time complexity: O(n^2), since each cell is visited once and each move checks three directions.
  • 🧺 Space complexity: O(n^2), for the DP tables.