Gold Mine Problem

Pattern: 2D grid DP

medium

A miner starts in any row of the first column and moves right, right-up or right-down, collecting gold. Working from the last column backwards, each cell's best is its gold plus the best of the three cells it can move to.

The problem

A gold mine is a grid of gold amounts. A miner starts in any cell of the first column and may move to the cell on the right, right-up or right-down. Return the most gold collectable.

Think first

  • Which direction does the miner always move?
  • What does each cell need from the next column?
  • Where can the miner start?

Hints

    Brute force, and why it fails

    Try every path from every start: 3^cols.

    The key insight

    Columns are processed one at a time, each needing only the next column's best values.

    Dry run

    best3 4 4

    mat [[1, 3, 3], [2, 1, 4], [0, 6, 4]]. The last column: 3 4 4.

    Step 1 of 3

    The plan

    best = last column
    for columns from right to left: best[r] = mat[r][c] + max(best[r-1], best[r], best[r+1])
    return max(best)
    

    Complexity

    Time
    O(rows × cols)
    Space
    O(rows)

    Each cell checks three neighbours.

    Edge cases

    • One column.
    • One row.
    • All zeros.
    I've built my logic, show the code

    Java

    import java.util.*;
    
    class Solution {
        public int maxGold(int[][] mat) {
            int R = mat.length, C = mat[0].length;
            int[] best = new int[R];                // best[r]: the most gold collectable starting from (r, c) to the right edge
            for (int r = 0; r < R; r++) best[r] = mat[r][C - 1];
            for (int c = C - 2; c >= 0; c--) {      // work from the right: each step goes right, right-up or right-down
                int[] next = new int[R];
                for (int r = 0; r < R; r++) {
                    int m = best[r];
                    if (r > 0) m = Math.max(m, best[r - 1]);
                    if (r < R - 1) m = Math.max(m, best[r + 1]);
                    next[r] = mat[r][c] + m;
                }
                best = next;
            }
            return Arrays.stream(best).max().getAsInt();   // start in any row of the first column
        }
    }

    C++

    class Solution {
    public:
        int maxGold(vector<vector<int>>& mat) {
            int R = static_cast<int>(mat.size()), C = static_cast<int>(mat[0].size());
            vector<int> best(R);                    // best[r]: the most gold collectable starting from (r, c) to the right edge
            for (int r = 0; r < R; r++) best[r] = mat[r][C - 1];
            for (int c = C - 2; c >= 0; c--) {      // work from the right: each step goes right, right-up or right-down
                vector<int> next(R);
                for (int r = 0; r < R; r++) {
                    int m = best[r];
                    if (r > 0) m = max(m, best[r - 1]);
                    if (r < R - 1) m = max(m, best[r + 1]);
                    next[r] = mat[r][c] + m;
                }
                best = next;
            }
            return *max_element(best.begin(), best.end());   // start in any row of the first column
        }
    };

    Python

    class Solution:
        def maxGold(self, mat: list[list[int]]) -> int:
            R = len(mat)
            best = [row[-1] for row in mat]         # best[r]: the most gold from (r, c) to the right edge
            for c in range(len(mat[0]) - 2, -1, -1):   # each step goes right, right-up or right-down
                best = [mat[r][c] + max(best[max(r - 1, 0):r + 2]) for r in range(R)]
            return max(best)                        # start in any row of the first column

    Common mistakes

    • Moving left or vertically.
    • Starting only from row 0.

    Similar problems