Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Break down a hard problem into reliable checkpoints, edge-case handling, and complexity trade-offs.
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:
2 <= board.length == board[i].length <= 100Problem summary: 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].
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Dynamic Programming
["E23","2X2","12S"]
["E12","1X1","21S"]
["E11","XXX","11S"]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1301: Number of Paths with Max Score
class Solution {
private List<String> board;
private int n;
private int[][] f;
private int[][] g;
private final int mod = (int) 1e9 + 7;
public int[] pathsWithMaxScore(List<String> board) {
n = board.size();
this.board = board;
f = new int[n][n];
g = new int[n][n];
for (var e : f) {
Arrays.fill(e, -1);
}
f[n - 1][n - 1] = 0;
g[n - 1][n - 1] = 1;
for (int i = n - 1; i >= 0; --i) {
for (int j = n - 1; j >= 0; --j) {
update(i, j, i + 1, j);
update(i, j, i, j + 1);
update(i, j, i + 1, j + 1);
if (f[i][j] != -1) {
char c = board.get(i).charAt(j);
if (c >= '0' && c <= '9') {
f[i][j] += (c - '0');
}
}
}
}
int[] ans = new int[2];
if (f[0][0] != -1) {
ans[0] = f[0][0];
ans[1] = g[0][0];
}
return ans;
}
private void update(int i, int j, int x, int y) {
if (x >= n || y >= n || f[x][y] == -1 || board.get(i).charAt(j) == 'X'
|| board.get(i).charAt(j) == 'S') {
return;
}
if (f[x][y] > f[i][j]) {
f[i][j] = f[x][y];
g[i][j] = g[x][y];
} else if (f[x][y] == f[i][j]) {
g[i][j] = (g[i][j] + g[x][y]) % mod;
}
}
}
// Accepted solution for LeetCode #1301: Number of Paths with Max Score
func pathsWithMaxScore(board []string) []int {
n := len(board)
f := make([][]int, n)
g := make([][]int, n)
for i := range f {
f[i] = make([]int, n)
g[i] = make([]int, n)
for j := range f[i] {
f[i][j] = -1
}
}
f[n-1][n-1] = 0
g[n-1][n-1] = 1
const mod = 1e9 + 7
update := func(i, j, x, y int) {
if x >= n || y >= n || f[x][y] == -1 || board[i][j] == 'X' || board[i][j] == 'S' {
return
}
if f[x][y] > f[i][j] {
f[i][j] = f[x][y]
g[i][j] = g[x][y]
} else if f[x][y] == f[i][j] {
g[i][j] = (g[i][j] + g[x][y]) % mod
}
}
for i := n - 1; i >= 0; i-- {
for j := n - 1; j >= 0; j-- {
update(i, j, i+1, j)
update(i, j, i, j+1)
update(i, j, i+1, j+1)
if f[i][j] != -1 && board[i][j] >= '0' && board[i][j] <= '9' {
f[i][j] += int(board[i][j] - '0')
}
}
}
ans := make([]int, 2)
if f[0][0] != -1 {
ans[0], ans[1] = f[0][0], g[0][0]
}
return ans
}
# Accepted solution for LeetCode #1301: Number of Paths with Max Score
class Solution:
def pathsWithMaxScore(self, board: List[str]) -> List[int]:
def update(i, j, x, y):
if x >= n or y >= n or f[x][y] == -1 or board[i][j] in "XS":
return
if f[x][y] > f[i][j]:
f[i][j] = f[x][y]
g[i][j] = g[x][y]
elif f[x][y] == f[i][j]:
g[i][j] += g[x][y]
n = len(board)
f = [[-1] * n for _ in range(n)]
g = [[0] * n for _ in range(n)]
f[-1][-1], g[-1][-1] = 0, 1
for i in range(n - 1, -1, -1):
for j in range(n - 1, -1, -1):
update(i, j, i + 1, j)
update(i, j, i, j + 1)
update(i, j, i + 1, j + 1)
if f[i][j] != -1 and board[i][j].isdigit():
f[i][j] += int(board[i][j])
mod = 10**9 + 7
return [0, 0] if f[0][0] == -1 else [f[0][0], g[0][0] % mod]
// Accepted solution for LeetCode #1301: Number of Paths with Max Score
/**
* [1301] 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:
*
*
* 2 <= board.length == board[i].length <= 100
*
*/
pub struct Solution {}
// problem: https://leetcode.com/problems/number-of-paths-with-max-score/
// discuss: https://leetcode.com/problems/number-of-paths-with-max-score/discuss/?currentPage=1&orderBy=most_votes&query=
// submission codes start here
impl Solution {
// Credit: https://leetcode.com/problems/number-of-paths-with-max-score/solutions/3096223/just-a-runnable-solution/
pub fn paths_with_max_score(board: Vec<String>) -> Vec<i32> {
let dirs = vec![vec![1, 0], vec![0, 1], vec![1, 1]];
let sz = board.len();
let mut score = vec![vec![0; sz + 1]; sz + 1];
let mut paths = vec![vec![0; sz + 1]; sz + 1];
let mut board = board;
board[0].replace_range(..1, "0");
board[sz - 1].replace_range(sz - 1.., "0");
paths[0][0] = 1;
for i in 1..=sz {
for j in 1..=sz {
let board_ij = board[i - 1].chars().nth(j - 1).unwrap();
if board_ij == 'X' {
continue;
}
for d in &dirs {
let i1 = i - d[0];
let j1 = j - d[1];
let val = score[i1][j1] + (board_ij as i32 - '0' as i32);
if score[i][j] <= val && paths[i1][j1] > 0 {
paths[i][j] = (if score[i][j] == val { paths[i][j] } else { 0 }
+ paths[i1][j1])
% 1000000007;
score[i][j] = val;
}
}
}
}
let val = paths[sz][sz];
vec![if val != 0 { score[sz][sz] } else { 0 }, val]
}
}
// submission codes end
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_1301_example_1() {
let board = vec_string!["E23", "2X2", "12S"];
let result = vec![7, 1];
assert_eq!(Solution::paths_with_max_score(board), result);
}
#[test]
fn test_1301_example_2() {
let board = vec_string!["E12", "1X1", "21S"];
let result = vec![4, 2];
assert_eq!(Solution::paths_with_max_score(board), result);
}
#[test]
fn test_1301_example_3() {
let board = vec_string!["E11", "XXX", "11S"];
let result = vec![0, 0];
assert_eq!(Solution::paths_with_max_score(board), result);
}
}
// Accepted solution for LeetCode #1301: Number of Paths with Max Score
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #1301: Number of Paths with Max Score
// class Solution {
// private List<String> board;
// private int n;
// private int[][] f;
// private int[][] g;
// private final int mod = (int) 1e9 + 7;
//
// public int[] pathsWithMaxScore(List<String> board) {
// n = board.size();
// this.board = board;
// f = new int[n][n];
// g = new int[n][n];
// for (var e : f) {
// Arrays.fill(e, -1);
// }
// f[n - 1][n - 1] = 0;
// g[n - 1][n - 1] = 1;
// for (int i = n - 1; i >= 0; --i) {
// for (int j = n - 1; j >= 0; --j) {
// update(i, j, i + 1, j);
// update(i, j, i, j + 1);
// update(i, j, i + 1, j + 1);
// if (f[i][j] != -1) {
// char c = board.get(i).charAt(j);
// if (c >= '0' && c <= '9') {
// f[i][j] += (c - '0');
// }
// }
// }
// }
// int[] ans = new int[2];
// if (f[0][0] != -1) {
// ans[0] = f[0][0];
// ans[1] = g[0][0];
// }
// return ans;
// }
//
// private void update(int i, int j, int x, int y) {
// if (x >= n || y >= n || f[x][y] == -1 || board.get(i).charAt(j) == 'X'
// || board.get(i).charAt(j) == 'S') {
// return;
// }
// if (f[x][y] > f[i][j]) {
// f[i][j] = f[x][y];
// g[i][j] = g[x][y];
// } else if (f[x][y] == f[i][j]) {
// g[i][j] = (g[i][j] + g[x][y]) % mod;
// }
// }
// }
Use this to step through a reusable interview workflow for this problem.
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.