- Define precisely what one DP state represents.
- Establish the base cases before transitions are evaluated.
- Process states in dependency order and combine only already-known values.
Code notes
- 41 lines of Java from the credited upstream file 1301.java.
- The implementation visibly relies on sequence storage, cached states.
- 3 loop blocks detected.
Complexity
Multiply the number of reachable states by the work performed for each transition, then include the stored state table in memory usage.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
1class Solution {2 public int[] pathsWithMaxScore(List<String> board) {3 final int MOD = 1_000_000_007;4 final int[][] DIRS = {{0, 1}, {1, 0}, {1, 1}};5 final int n = board.size();6 7 int[][] dp = new int[n + 1][n + 1];8 Arrays.stream(dp).forEach(A -> Arrays.fill(A, -1));9 10 int[][] count = new int[n + 1][n + 1];11 12 dp[0][0] = 0;13 dp[n - 1][n - 1] = 0;14 count[n - 1][n - 1] = 1;15 16 for (int i = n - 1; i >= 0; --i)17 for (int j = n - 1; j >= 0; --j) {18 if (board.get(i).charAt(j) == 'S' || board.get(i).charAt(j) == 'X')19 continue;20 for (int[] dir : DIRS) {21 final int x = i + dir[0];22 final int y = j + dir[1];23 if (dp[i][j] < dp[x][y]) {24 dp[i][j] = dp[x][y];25 count[i][j] = count[x][y];26 } else if (dp[i][j] == dp[x][y]) {27 count[i][j] += count[x][y];28 count[i][j] %= MOD;29 }30 }31 32 if (dp[i][j] != -1 && board.get(i).charAt(j) != 'E') {33 dp[i][j] += board.get(i).charAt(j) - '0';34 dp[i][j] %= MOD;35 }36 }37 38 return new int[] {dp[0][0], count[0][0]};39 }40}41