Gold Mine Problem | Practice | GeeksforGeeks

Given a gold mine called M of (n x m) dimensions. 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

to the cell diagonally up towards the right 
to the right
to the cell diagonally down towards the right
Find out maximum amount of gold which he can collect.

Example 1:
Input: n = 3, m = 3
M = {{1, 3, 3},
     {2, 1, 4},
     {0, 6, 4}};
Output: 12
Explaination: 
The path is {(1,0) -> (2,1) -> (2,2)}.

Example 2:
Input: n = 4, m = 4
M = {{1, 3, 1, 5},
     {2, 2, 4, 1},
     {5, 0, 2, 3},
     {0, 6, 1, 2}};
Output: 16
Explaination: 
The path is {(2,0) -> (3,1) -> (2,2) 
-> (2,3)} or {(2,0) -> (1,1) -> (1,2) 
-> (0,3)}.

Your Task:
You do not need to read input or print anything. Your task is to complete the function maxGold() which takes the values n, m and the mine M as input parameters and returns the maximum amount of gold that can be collected.

Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)

Constraints:
1 ≤ n, m ≤ 50
1 ≤ M[i][j] ≤ 100
0
rohit jain 111 week ago
Please help, on submitting my solution i am getting wrong result for following input: [{2,1}{1,2}]. I cant find error in my algo.
 
public static int maxGold(int n, int m, int M[][]) {   return maxGoldCal(n, m, M);}
public static int maxGoldCal(int n, int m, int M[][]) {   int max = 0;   for (int j = m - 1; j >= 0; j--) {      for (int i = n - 1; i >= 0; i--) {         // cannot proceed condition         int previousRow = i - 1;         int nextRow = i + 1;         int nextColumn = j + 1;         boolean nextRowNotPresent = nextRow > (n - 1);         boolean previousRowNotPresent = previousRow < 0;         boolean nextColumnNotPresent = nextColumn > (m - 1);         boolean diagonalDownNotPossible = nextRowNotPresent || nextColumnNotPresent;         boolean diagonalUpNotPossible = previousRowNotPresent || nextColumnNotPresent;         if (nextColumnNotPresent) {            continue;         } else if (diagonalDownNotPossible) {            M[i][j] += Integer.max(M[previousRow][nextColumn], M[i][nextColumn]);         } else if (diagonalUpNotPossible) {            M[i][j] += Integer.max(M[i][nextColumn], M[nextRow][nextColumn]);         } else {            int temp = Integer.max(M[previousRow][nextColumn], M[nextRow][nextColumn]);            M[i][j] += Integer.max(temp, M[i][nextColumn]);         }         max = Integer.max(M[i][j], max);      }   }   return max;}
0
2020ucs00972 weeks ago
class Solution{public:   int max(int a, int b){       if(a>=b) return a;       else return b;   }   int f(int cn, int cm, int n, int m, vector<vector<int>> &M, vector<vector<int>> &v){       if(cn<0 || cn>n-1 || cm>m-1) return 0;       if(v[cn][cm]!=-1) return v[cn][cm];       int p,q,r;       p=f(cn-1,cm+1,n,m,M,v);       q=f(cn,cm+1,n,m,M,v);       r=f(cn+1,cm+1,n,m,M,v);       return v[cn][cm]=M[cn][cm]+ max(p,max(q,r));   }   int maxGold(int n, int m, vector<vector<int>> M)   {       int max=0,t;       vector<vector<int>> v;       v.resize(n,vector<int>(m,-1));       for (int i=0;i<n;i++){           t=f(i,0,n,m,M,v);           if(t>max) max=t;       }       return max;   }};
0
apurvkumarak2 weeks ago
C++ Solution 
    int solve(int n,int m,vector<vector<int>> & mat,vector<vector<int>> & dp,int i ,int j){
        if(j==m-1){
            dp[i][j]=mat[i][j];
            return mat[i][j];
        }
        if(dp[i][j] != -1){
            return dp[i][j];
        }
        int a=0,b=0,c=0;
        if(i-1 >= 0 && j+1 < m){
            if(dp[i-1][j+1] == -1){
                a=solve(n,m,mat,dp,i-1,j+1);
            }else{
                a=dp[i-1][j+1];
            }
        }
        if(j+1 < m){
            if(dp[i][j+1] == -1){
                b=solve(n,m,mat,dp,i,j+1);
            }else{
                b=dp[i][j+1];
            }
        }
        if(i+1 < n && j+1 < m){
            if(dp[i+1][j+1] == -1){
                c=solve(n,m,mat,dp,i+1,j+1);
            }else{
                c=dp[i+1][j+1];
            }
        }
        dp[i][j]=mat[i][j]+max(max(a,b),c);
        return dp[i][j];
    }
    int maxGold(int n, int m, vector<vector<int>> M)
    {
        vector<vector<int>> dp(n,vector<int>(m,-1));
        int res=INT_MIN;
        for(int i=0;i<M.size();i++){
            res=max(solve(n,m,M,dp,i,0),res);
        }
        return res;
    }
0
dwivedidivyanshu30Premium2 weeks ago
Why does this code give error ?
 
int maxGold(int n, int m, vector<vector<int>> M)
    {
       
        for(int col = m-2;col>=0;col--){
            for(int row = 0;row<n;row++){
                int a,b,c = 0;
                if((col+1)<m)
                a = M[row][col+1];
                if((row-1)>=0 and (col+1)<m)
                b = M[row-1][col+1];
                if((row+1<n)and (col+1)<m)
                c = M[row+1][col+1];
                M[row][col]+= max(a,max(b,c));
            }
        }
        int max = INT_MIN;
        for(int i=0;i<n;i++){
            if(M[i][0]>max)
            max = M[i][0];
        }
        return max;
    }
+1
kashyapjhon3 weeks ago
C++ Solution Pep Coding Method Time=(0.04/1.29) :
int maxGold(int n, int m, vector<vector<int>> M)   {       // code here       if(n>1){       int dp[n+2][m+2];       for(int j=m-1;j>=0;j--){           for(int i=0;i<n;i++){               if(j==m-1){                   dp[i][j]=M[i][j];               }               else if(i==0){                   dp[i][j]=M[i][j]+max(M[i][j+1],M[i+1][j+1]);                   M[i][j]=dp[i][j];               }               else if(i==n-1){                   dp[i][j]=M[i][j]+max(M[i-1][j+1],M[i][j+1]);                   M[i][j]=dp[i][j];               }               else{                   dp[i][j]=M[i][j]+max(M[i-1][j+1],max(M[i][j+1],M[i+1][j+1]));                   M[i][j]=dp[i][j];               }           }       }       int ans=INT_MIN;       for(int i=0;i<n;i++){           ans=max(ans,dp[i][0]);       }       return ans;       }       else{           int ans=0;           for(int i=0;i<m;i++){               ans=ans+M[0][i];           }           return ans;       }   }
0
omkarg14171 month ago
Time Complexity : O(n*m)
Space Complexity: O(2*n)
 
int maxGold(int n, int m, vector<vector<int>> a)
    {

        vector<vector<int>> dp(2, vector<int>(n, 0));
        for(int i = 0; i < n; ++i) {
            dp[0][i] = a[i][0];
        }
        
        int x = 1;
        
        for(int k = 1; k < m; ++k) {
            for(int i = 0; i < n; ++i) {
                int res1 = 0, res2 = 0;
                
                if(i-1 >= 0)
                    res1 = dp[1-x][i-1];
                if(i+1 < n) 
                    res2 = dp[1-x][i+1];
                
                dp[x][i] = a[i][k] + max({res1, res2, dp[1-x][i]});
            }
            x ^= 1;
        }
        
        int res=0;
        for(int i = 0; i < n; ++i) {
            res = max({res, dp[0][i], dp[1][i]});
        }
        return res;
 
0
shreeshsingh2 months ago
# User function Template for Python3
from typing import List


# filling dp table from left to right

# 2D dp tabulation

class Solution:
    def maxGold(self, n: int, m: int, matrix: List[List[int]]) -> int:
        # code here
 
        
        dp = [ [0 for _ in range(m+1)] for _ in range(n+1) ]
        
        for j in range(m):
            for i in range(n):
                
                left = 0
                leftUp = 0
                leftDown = 0
                
                if j - 1 <= m:
                    left = dp[i][j-1]
                    
                if i - 1 >= 0 and j - 1 <= m:
                    leftUp = dp[i-1][j-1]
                    
                if i+1 <= n and j - 1 <= m:
                    leftDown = dp[i+1][j-1]
                    
                dp[i][j] = matrix[i][j] + max(left, leftUp, leftDown)
                    
        res = dp[0][m-1]
        
        for i in range(1, n):
            res = max(res, dp[i][m-1])
            
        return res
+1
aloksinghbais022 months ago
C++ solution having time complexity as O(n*m) and space complexity as O(n*m) is as follows :- 
 
Execution Time :- 0.0 / 1.3 sec
 
int dp[51][51];      bool isValid(int x,int y,int n,int m){       if(x < 0 || x >= n || y < 0 || y >= m) return (false);       return (true);   }      int helper(int x,int y,int n,int m,vector<vector<int>> &M){       if(!isValid(x,y,n,m)) return (0);              if(dp[x][y] != -1) return (dp[x][y]);              int res1 = M[x][y] + helper(x-1,y+1,n,m,M);       int res2 = M[x][y] + helper(x,y+1,n,m,M);       int res3 = M[x][y] + helper(x+1,y+1,n,m,M);       return dp[x][y] = (max(res1,max(res2,res3)));   }      int maxGold(int n, int m, vector<vector<int>> M)   {       int maxGold = 0;       memset(dp,-1,sizeof(dp));              for(int i = 0; i < n; i++){           int gold = helper(i,0,n,m,M);           maxGold = max(maxGold,gold);       }              return (maxGold);   }
0
tarun2002ts0212 months ago
3 / 204Input: 
2 1 1 2
And Your Code's output is:
1Its Correct output is:
2
 
i think both “correct output ” ans “ your output” is wrong i think right ans will be 4 
 my code--- have a look
 int maxGold(int n, int m, vector<vector<int>> M)   {       // code here       int maxi=INT_MIN;       vector<vector<int>>dp(n+2,vector<int>(m+2,-1));       for(int i=0;i<m;i++)          {maxi=max(maxi,solve(i,0,n,m,M,dp));}       return maxi;   }
 
//////////////////////function 1   int solve(int i,int j,int n,int m,vector<vector<int>>&arr,vector<vector<int>>dp)   {       if(!satisfy(i,j,n,m))       return 0;       if(dp[i][j]!=-1)       return dp[i][j];              return dp[i][j]=arr[i][j]+max(solve(i,j+1,n,m,arr,dp),max(solve(i-1,j+1,n,m,arr,dp),solve(i+1,j+1,n,m,arr,dp)));   }
 
////////////////// function2   bool satisfy(int i,int j,int n,int m)   {       if(i<0 || j<0 ||i>=n|| j>=m)       return false;       return true;   }
///////////function 3
 
 
 
 
0
helloworld013
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.