Number of Paths with Max Score - Problem
Number of Paths with Max Score

Imagine you're navigating a treasure-filled dungeon represented as a square grid! You start at the bottom-right corner marked with 'S' and need to reach the top-left corner marked with 'E'.

Each cell contains either:
โ€ข A treasure value (digits 1-9) that you can collect
โ€ข An obstacle marked with 'X' that blocks your path

You can move in three directions: up, left, or diagonally up-left. Your goal is to find the path that maximizes your treasure collection!

Return: A list with two integers:
1. The maximum sum of treasure you can collect
2. The number of different paths that achieve this maximum sum (modulo 109 + 7)

If no path exists, return [0, 0].

Input & Output

example_1.py โ€” Basic Path Finding
$ Input: board = ["E23", "2X2", "12S"]
โ€บ Output: [7, 1]
๐Ÿ’ก Note: There is one path: S(0) โ†’ 1(1) โ†’ 2(3) โ†’ 2(5) โ†’ 3(8) โ†’ E. The maximum score is 7 and there's exactly 1 path achieving this score.
example_2.py โ€” Multiple Optimal Paths
$ Input: board = ["E12", "1X1", "21S"]
โ€บ Output: [4, 2]
๐Ÿ’ก Note: There are two optimal paths: S(0) โ†’ 1(1) โ†’ 2(3) โ†’ 1(4) โ†’ E and S(0) โ†’ 2(2) โ†’ 1(3) โ†’ 1(4) โ†’ E. Both achieve maximum score of 4.
example_3.py โ€” No Valid Path
$ Input: board = ["E11", "XXX", "11S"]
โ€บ Output: [0, 0]
๐Ÿ’ก Note: All paths from S to E are blocked by obstacles 'X', so there's no valid path. Return [0, 0].

Constraints

  • 2 โ‰ค board.length == board[i].length โ‰ค 100
  • board[i][j] is one of 'E', 'S', '1', '2', ..., '9', or 'X'
  • It is guaranteed that board[0][0] == 'E' and board[n-1][n-1] == 'S'
  • Answer fits in 32-bit signed integer

Visualization

Tap to expand
EScore: 0Paths: 13Score: 3Paths: 15Score: 8Paths: 12Score: 2Paths: 1XBLOCKED1Score: 6Paths: 14Score: 6Paths: 16Score: 12Paths: 1SScore: 16Paths: 1๐Ÿดโ€โ˜ ๏ธ Optimal Treasure RoutePath: S(0) โ†’ 6(6) โ†’ 4(10) โ†’ 2(12) โ†’ ETotal Treasure: 16 coinsNumber of optimal paths: 1๐Ÿ’ก DP builds solution backwards:Each room knows the best future treasure!
Understanding the Visualization
1
Set Base Camp
Mark the destination 'E' with score (0, 1) - no treasure needed here, one way to 'finish'
2
Map Adjacent Rooms
Calculate treasure scores for cells next to destination, considering all three possible approaches
3
Build Treasure Map
Continue row by row, each cell learns the optimal treasure and path count from future positions
4
Find Starting Strategy
The starting position 'S' now contains the maximum treasure and total optimal paths
Key Takeaway
๐ŸŽฏ Key Insight: Dynamic Programming transforms this exponential search into an efficient O(nยฒ) solution by building optimal substructure from destination to source, avoiding redundant path explorations.
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 25
48.2K Views
Medium-High Frequency
~25 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen