12. Gold Mine Problem
β GFG solution to the Gold Mine Problem: find maximum gold collection path from left to right using dynamic programming optimization. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given a gold mine called mat[][]
. Each field in this mine contains a positive integer which is the amount of gold in tons. Initially, the miner can start from any row in the first column. From a given cell, the miner can move:
to the cell diagonally up towards the right
to the right
to the cell diagonally down towards the right
Find out the maximum amount of gold that he can collect until he can no longer move.
π Examples
Example 1
Input: mat[][] = [[1, 3, 3], [2, 1, 4], [0, 6, 4]]
Output: 12
Explanation: The path is (1, 0) -> (2, 1) -> (2, 2). Total gold collected is 2 + 6 + 4 = 12.
Example 2
Input: mat[][] = [[1, 3, 1, 5], [2, 2, 4, 1], [5, 0, 2, 3], [0, 6, 1, 2]]
Output: 16
Explanation: The path is (2, 0) -> (3, 1) -> (2, 2) -> (2, 3) or (2, 0) -> (1, 1) -> (1, 2) -> (0, 3).
Total gold collected is (5 + 6 + 2 + 3) or (5 + 2 + 4 + 5) = 16.
Example 3
Input: mat[][] = [[1, 3, 3], [2, 1, 4], [0, 7, 5]]
Output: 14
Explanation: The path is (1,0) -> (2,1) -> (2,2). Total gold collected is 2 + 7 + 5 = 14.
π Constraints
$1 \le \text{mat.size()}, \text{mat[0].size()} \le 500$
$0 \le \text{mat}[i][j] \le 100$
β
My Approach
The optimal approach uses Dynamic Programming with in-place optimization to efficiently find the maximum gold collection path:
Bottom-Up Dynamic Programming
Initialize Strategy:
Start from the rightmost column (last column) as base case.
Work backwards towards the first column, column by column.
For each cell, calculate the maximum gold that can be collected from that position.
State Transition:
For each cell
mat[i][j]
, the maximum gold from this position is:mat[i][j] + max(gold from three possible next moves)
Three possible moves: diagonally up-right, right, diagonally down-right.
Boundary Handling:
Check if the next position is within matrix bounds.
Use only valid adjacent cells for maximum calculation.
Handle edge cases for first and last rows.
In-place Optimization:
Modify the original matrix to store maximum gold from each position.
No need for additional space to store DP values.
Result Extraction:
After processing all columns, the answer is the maximum value in the first column.
Any starting row in the first column can be chosen.
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(n Γ m), where n is the number of rows and m is the number of columns. We visit each cell exactly once and perform constant time operations for each cell.
Expected Auxiliary Space Complexity: O(1), as we perform the dynamic programming in-place by modifying the original matrix without using any additional space.
π§βπ» Code (C++)
class Solution {
public:
int maxGold(vector<vector<int>>& mat) {
int n = mat.size(), m = mat[0].size();
for (int j = m - 2; j >= 0; j--) {
for (int i = 0; i < n; i++) {
int mx = 0;
for (int di = -1; di <= 1; di++) {
int ni = i + di;
if (ni >= 0 && ni < n) mx = max(mx, mat[ni][j + 1]);
}
mat[i][j] += mx;
}
}
int res = 0;
for (int i = 0; i < n; i++) res = max(res, mat[i][0]);
return res;
}
};
π§βπ» Code (Java)
class Solution {
public int maxGold(int[][] mat) {
int n = mat.length, m = mat[0].length;
for (int j = m - 2; j >= 0; j--) {
for (int i = 0; i < n; i++) {
int mx = 0;
for (int di = -1; di <= 1; di++) {
int ni = i + di;
if (ni >= 0 && ni < n) mx = Math.max(mx, mat[ni][j + 1]);
}
mat[i][j] += mx;
}
}
int res = 0;
for (int i = 0; i < n; i++) res = Math.max(res, mat[i][0]);
return res;
}
}
π Code (Python)
class Solution:
def maxGold(self, mat):
n, m = len(mat), len(mat[0])
for j in range(m - 2, -1, -1):
for i in range(n):
mx = 0
for di in [-1, 0, 1]:
ni = i + di
if 0 <= ni < n:
mx = max(mx, mat[ni][j + 1])
mat[i][j] += mx
return max(mat[i][0] for i in range(n))
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated