Courier tolls
Standard · Write code · about 12 min
A courier crosses a city laid out as blocks. Entering block (r, c) costs tolls[r][c], the start block included. The courier starts at the top-left block, ends at the bottom-right one, and only moves east or south.
Return the smallest total toll. First decide which DP family this is.
Example 1
Input:
[[[1,3,1],[1,5,1],[4,2,1]]]
Output:
7
1 → 3 → 1 → 1 → 1, east twice then south twice.
Example 2
Input:
[[[1,2,3],[4,5,6]]]
Output:
12
Constraints
- 1 ≤ rows, cols ≤ 200
- 0 ≤ tolls[r][c] ≤ 100
Learn the idea first: Recognizing DP and choosing the state, a visual lesson.
More Recognizing DP and choosing the state problems
- Pick the state Warm-up
- Write the recurrence Standard
- Domino tilings Standard
- Walk the flowchart Standard
- Design a DP: paint the houses Challenge