0

In a gold mine grid of size m x n, each cell in this mine has an integer representing the amount of gold in that cell, 0 if it is empty.

Return the maximum amount of gold you can collect under the conditions:

Every time you are located in a cell you will collect all the gold in that cell. From your position, you can walk one step to the left, right, up, or down. You can't visit the same cell more than once. Never visit a cell with 0 gold. You can start and stop collecting gold from any position in the grid that has some gold.

class Solution {
    public int getMaximumGold( int[][] grid) {
         int max=0, min=0;
       for(int i=0; i<grid.length; i++){
           for(int j=0; j<grid[i].length; j++){
           
              min=gold(grid, i, j);
               max=Math.max(max, min);
               
           }
       }
       
       
        return max;
    }
    public int gold(int[][] grid,int row, int col) {
        if(grid[row][col]==0) return 0;
        int max=0, min=0; 
       int [][] newgrid = grid;
        int currgold = newgrid[row][col];
        
        newgrid[row][col]=0;
        //left check
        if(col-1>=0 && newgrid[row][col-1]!=0) {
            int[][] ogrid = newgrid;
            max=gold(ogrid,row,col-1);
        }
        //right check
        if(col+1<newgrid[row].length && newgrid[row][col+1]!=0) {
            int[][] ogrid = newgrid;
            min=gold(ogrid,row,col+1);
        }
        max=Math.max(max,min);
        //up check
        if(row-1>=0 && newgrid[row-1][col]!=0) {
            int[][] ogrid = newgrid;
            min=gold(ogrid,row-1,col);
            }
        max=Math.max(max,min);
        //downcheck
        if(row+1<newgrid.length && newgrid[row+1][col] != 0) {
            int[][] ogrid = newgrid;
            min = gold(ogrid, row+1, col);
        }
        
        return Math.max(max,min)+currgold;
    }
}

  • 1
    do you also have a question to ask? – Stultuske Nov 08 '22 at 08:28
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Nov 08 '22 at 12:11

0 Answers0