Maximum Ore Haul on a Climb-and-Step Grid Route
Dynamic ProgrammingAuto-graded · 23 test casesLast updated
Grid dynamic programming: find the route with the largest sum from the bottom-left to the top-right cell when only moving up or right.
Maximum Ore Haul
A miner explores a rectangular map stored as a 2D list of integers field with m rows and n columns. The value field[r][c] is the amount of ore lying in the cell at row r, column c. Row 0 is the top row and column 0 is the leftmost column.
The miner begins in the bottom-left cell, (m - 1, 0), and must finish in the top-right cell, (0, n - 1). From any cell (r, c) the miner may take one of two steps:
- Up to
(r - 1, c) - Right to
(r, c + 1)
Every cell the miner stands on, including the starting cell and the final cell, has its ore collected in full. Because every step goes up or right, no cell can be visited twice.
Return the largest total amount of ore that can be collected along any valid route from the start cell to the finish cell.
Constraints
m == len(field)n == len(field[0])1 <= m, n <= 10000 <= field[r][c] <= 10^5- If the grid is
1 x 1, the start and finish are the same cell, and the answer is that cell's value. - If the grid has a single row or a single column, exactly one route exists.
Examples
Example 1
Input: max_ore_haul([[0, 2, 5], [1, 1, 3], [2, 1, 1]])
Output: 12
Example 2
Input: max_ore_haul([[1, 3], [2, 4]])
Output: 9
Example 3
Input: max_ore_haul([[7]])
Output: 7
Hints
Hint 1
Every route enters a cell from only one of two neighbors: the cell directly below it or the cell directly to its left.
Hint 2
Define best[r][c] as the maximum ore collectable on a route from the start that ends at (r, c), and express it using best of the two possible predecessors.
Hint 3
Process rows from the bottom to the top and columns left to right so both predecessors are ready. A single array of length n is enough since each row only needs the row beneath it.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Every route consists of up and right moves only, so any cell (r, c) is entered either from the cell below it, (r + 1, c), or from the cell to its left, (r, c - 1). Let best[r][c] be the maximum ore collectable on a route from the start that ends at (r, c). Then:
best[r][c] = field[r][c] + max(best[r+1][c], best[r][c-1])
with these boundary rules:
- The start cell
(m-1, 0)hasbest = field[m-1][0]. - In the bottom row, a cell can only be reached from the left.
- In the leftmost column, a cell can only be reached from below.
The answer is best[0][n-1].
Evaluation order and space
Each cell depends on the cell below and the cell to its left, so we process rows from bottom to top and columns left to right. Row r needs only row r + 1 plus the part of row r already computed, so one array dp of length n suffices. When we reach column c, dp[c] still holds the value from the row below, and dp[c-1] has already been updated for the current row (the value from the left). We overwrite dp[c] in place.
Why it is correct
- Any optimal route to a cell must reach its predecessor by an optimal route, otherwise swapping in a better prefix would improve it.
- Every route to a cell has a last move, either up or right, and we take the maximum over both, so all routes are covered.
- Moves are monotone, so no cell is counted twice, and ore values are non-negative.
- By induction over the processing order, every value is final when it is read.
Complexity
- Time:
O(m * n). - Space:
O(n)extra.
Reference solution
from typing import List
def max_ore_haul(field: List[List[int]]) -> int:
m, n = len(field), len(field[0])
dp = [0] * n
for r in range(m - 1, -1, -1):
for c in range(n):
from_below = dp[c] if r < m - 1 else 0
from_left = dp[c - 1] if c > 0 else 0
if r == m - 1 and c == 0:
dp[c] = field[r][c]
elif r == m - 1:
dp[c] = from_left + field[r][c]
elif c == 0:
dp[c] = from_below + field[r][c]
else:
dp[c] = max(from_below, from_left) + field[r][c]
return dp[n - 1]