VOOZH about

URL: https://www.geeksforgeeks.org/dsa/gold-mine-problem/

⇱ Gold Mine Problem - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Gold Mine Problem

Last Updated : 8 Jul, 2025

Given a gold mine represented as a 2d matrix mat[][], where each cell contains a positive integer indicating the amount of gold (in tons) available in that field. The miner can start from any row in the first column. From any cell, the miner is allowed to move in one of the following three directions:

  • Diagonally up-right.
  • Directly right.
  • Diagonally down-right.

The miner continues moving in this way until reaching the last column or until no further move is possible. Find the maximum amount of gold the miner can collect following these rules.

Examples:

Input: mat[][] = [[1, 3, 3],
[2, 1, 4],
[0, 6, 4]]
Output: 12
Explanation: The miner starts from the second row of the first column (collecting 2 tons of gold), moves to the third row of the second column (collecting 6 tons of gold), and then to the second row of the third column (collecting 4 tons of gold), for a total of 2 + 6 + 4 = 12 tons of gold collected.

Input: mat[][] = [[1, 3, 1, 5],
[2, 2, 4, 1],
[5, 0, 2, 3],
[0, 6, 1, 2]];
Output: 16
Explanation: The miner starts from the third row of the first column (collecting 5 tons of gold), moves to the second row of the second column (collecting 2 tons), then to the second row of the third column (collecting 4 tons), and finally to the first row of the fourth column (collecting 5 tons), for a total of 5 + 2 + 4 + 5 = 16 tons of gold collected.

[Naive Approach] Recursive DFS

The idea is to try all possible paths starting from each cell in the first column using recursion.
From each cell (x, y), we recursively call three directions: collectGold(x-1, y+1) -> right upper , collectGold(x, y+1) -> right , and collectGold(x+1, y+1) -> right lower, to explore all valid moves and take the maximum.


Output
16

Time Complexity: O(3^(n * m)), from each cell, 3 recursive calls are made, leading to exponential growth.
Auxiliary Space: O(n × m) due to recursion stack in the worst case (depth can go up to m steps, each from different rows).

[Better approach 1] Top-Down Dynamic Programming (Memoization) - O(n×m) Time and O(n×m) Space

The idea is to start from each cell in the first column and explore all valid paths using dfs.
At each step (x, y), we try three moves: collectGold(x-1, y+1) -> right upper , collectGold(x, y+1) -> right , and collectGold(x+1, y+1) -> right lower.
We store results in a memo table to avoid recalculating overlapping subproblems.


Output
16

[Better approach 2] Bottom-Up Dynamic Programming (Tabulation) - O(n×m) Space and O(n×m) Space

We define dp[x][y] as the maximum gold that can be collected starting from cell (x, y).
At each cell, we consider three possible moves: right upper (x-1, y+1), right (x, y+1), and right lower (x+1, y+1).
So the relation becomes: dp[x][y] = mat[x][y] + max(dp[x-1][y+1], dp[x][y+1], dp[x+1][y+1]), ensuring all indices are within bounds.


Output
16

[Efficient approach] Space-Optimized Tabulation – O(n×m) Time and O(n) Space

Instead of storing the entire dp[n][m] table, we only track gold values for the current and next columns using two 1D arrays: prev and curr.
We iterate from right to left, updating curr[x] using prev[x-1] -> right-upper, prev[x] -> right, and prev[x+1] -> right-lower, then swap prev and curr for the next round.
The state is given by: curr[x] = mat[x][y] + max(prev[x-1], prev[x], prev[x+1]), ensuring all indices are within bounds.


Output
16

[Expected Approach] In-place Dynamic Programming from Right to Left - O(n×m) Time ans O(1) Space

The idea is to use in-place tabulation, traversing the matrix from right to left. For each cell mat[x][y], we calculate the maximum gold that can be collected if we come from one of the three cells in the next (right) column: mat[x-1][y+1] → right-upper, mat[x][y+1] → right, mat[x+1][y+1] → right-lower.
We update the current cell as: mat[x][y] = mat[x][y] + max(mat[x-1][y+1], mat[x][y+1], mat[x+1][y+1]).
This update is safe because we are moving right to left, so any update to mat[x][y] does not affect future calculations — the values to the right (which we depend on) have already been processed and won’t be modified again.


Output
16
Comment