Grid Path Toll Minimiser
Problem Description
You are given an `rows x cols` grid of non-negative integers called `tollGrid`, where `tollGrid[r][c]` represents the toll you must pay when you enter cell `(r, c)`.
Starting from the **top-left cell** `(0, 0)`, you want to reach the **bottom-right cell** `(rows-1, cols-1)`. At each step you may move **right** or **down** only.
Return the minimum total toll to travel from `(0, 0)` to `(rows-1, cols-1)`.
**Example 1:**
```
Input: tollGrid = [
[1, 3, 2],
[4, 1, 5],
[2, 6, 1]
]
Output: 7
Explanation: Path (0,0)→(0,1)→(1,1)→(2,1)→(2,2) gives toll 1+3+1+6+1=12.
Path (0,0)→(1,0)→(1,1)→(1,2)→(2,2) gives toll 1+4+1+5+1=12.
Path (0,0)→(0,1)→(0,2)→(1,2)→(2,2) gives toll 1+3+2+5+1=12.
Optimal path (0,0)→(1,0)→(2,0)→(2,1)→(2,2) gives toll 1+4+2+6+1=14.
Actually optimal is (0,0)→(0,1)→(1,1)→(1,2)→(2,2) = 1+3+1+5+1 = 11? No.
Correct optimal: (0,0)→(1,0)→(1,1)→(1,2)→(2,2) = 1+4+1+5+1 = 12.
Re-check: (0,0)→(0,1)→(1,1)→(2,1) is invalid, can't go further down then right to (2,2): 1+3+1+6+1=12.
Minimum is 7: path (0,0)→(1,0)→(1,1)→(1,2)→(2,2) costs 1+4+1+5+1=12? Let me recount.
With grid [[1,3,2],[4,1,5],[2,6,1]] the answer is 10: (0,0)+(1,0)+(1,1)+(1,2)+(2,2)=1+4+1+5+1=12?
Actually output = 10. Path: (0,0)=1, right→(0,1)=3, down→(1,1)=1, down→(2,1)=6, right→(2,2)=1 = 12. Or down→(1,0)=4, down→(2,0)=2, right→(2,1)=6, right→(2,2)=1 = 1+4+2+6+1=14. Minimum verified = 10 via (0,0)→(0,1)→(1,1)→(1,2)→(2,2)=1+3+1+5+1=11. Let me use a cleaner example.
```
**Example 1:**
```
Input: tollGrid = [
[2, 5, 3],
[1, 1, 8],
[4, 2, 1]
]
Output: 8
Explanation: Optimal path (0,0)→(1,0)→(1,1)→(2,1)→(2,2) costs 2+1+1+2+1 = 7. Wait that is 7.
```
Let me provide clean verified examples:
**Example 1:**
```
Input: tollGrid = [[2, 5, 3],[1, 1, 8],[4, 2, 1]]
Output: 7
Explanation: Path (0,0)→(1,0)→(1,1)→(2,1)→(2,2) = 2+1+1+2+1 = 7.
```
**Example 2:**
```
Input: tollGrid = [[1, 2],[3, 4]]
Output: 7
Explanation: Path (0,0)→(0,1)→(1,1) = 1+2+4 = 7; path (0,0)→(1,0)→(1,1) = 1+3+4 = 8. Minimum is 7.
```
Constraints
- 1 <= rows, cols <= 200
- 0 <= tollGrid[r][c] <= 100
Follow-up
Can you also reconstruct one optimal path (list of cell coordinates)?
Hints
Try the problem first. If you get stuck, you can reveal hints one at a time.
Solution
Leaderboard
No entries yet for python.
Be the first — submit an accepted solution.