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
Each cell contains either:
โข A treasure value (digits 1-9) that you can collect
โข An obstacle marked with
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
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 pathYou 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
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.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code