Quantcast
Channel: Coddicted
Viewing all articles
Browse latest Browse all 10

Solved! Leetcode 1219. Path with Maximum Gold

$
0
0

source: https://leetcode.com/problems/path-with-maximum-gold/description/

Description: Path with Maximum Gold

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.

Example 1

<strong>Input:</strong> grid = [[0,6,0],[5,8,7],[0,9,0]]
<strong>Output:</strong> 24
<strong>Explanation:</strong>
[[0,6,0],
 [5,8,7],
 [0,9,0]]
Path to get the maximum gold, 9 -&gt; 8 -&gt; 7.

Example 2

<strong>Input:</strong> grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
<strong>Output:</strong> 28
<strong>Explanation:</strong>
[[1,0,7],
 [2,0,6],
 [3,4,5],
 [0,3,0],
 [9,0,20]]
Path to get the maximum gold, 1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 5 -&gt; 6 -&gt; 7.

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 <= m, n <= 15
  • 0 <= grid[i][j] <= 100
  • There are at most 25 cells containing gold.

Solution

class Solution {
    public int getMaximumGold(int&#91;]&#91;] grid) {
        if(grid==null || grid.length==0){
            return -1;
        }
        // starting from each cell, calculate the path sum and return the maximum sum
        // for the given grid &#91;&#91;0,6,0],&#91;5,8,7],&#91;0,9,0]]
        // for cell(0,1), there are 3 valid paths
        // 1. 6 -&gt; 8 -&gt; 5 = 19
        // 2. 6 -&gt; 8 -&gt; 9 = 23
        // 3. 6 -&gt; 8 -&gt; 7 = 21
        // max = 23
        // we need to find the max path sum for each cell and return the max
        int m = grid.length;
        int n = grid&#91;0].length;
        int max = 0;
        for(int i=0;i&lt;m;i++){
            for(int j=0;j&lt;n;j++){
                if(grid&#91;i]&#91;j]!=0){
                    max = Math.max(max, dfs(grid, i, j, m, n));
                }
            }
        }
        return max;
    }

    private int dfs(int&#91;]&#91;] grid, int r, int c, int m, int n){
        int&#91;]&#91;] xy = {{-1, 0}, {1, 0}, {0, 1}, {0,-1}};
        int max = 0;

        for(int i=0;i&lt;xy.length;i++){
            int newR = xy&#91;i]&#91;0] + r;
            int newC = xy&#91;i]&#91;1] + c;

            if(isValid(grid, newR, newC, m , n)){

                int temp = grid&#91;r]&#91;c];
                grid&#91;r]&#91;c]= 0;
                max = Math.max(max,  dfs(grid, newR, newC, m , n));
                //backtrack
                grid&#91;r]&#91;c] = temp;
            }

        }

        return grid&#91;r]&#91;c] +   max;
    }

    private boolean isValid(int&#91;]&#91;] grid, int r, int c, int m , int n){
        return r &gt;=0 &amp;&amp; r &lt; m 
            &amp;&amp; c &gt;=0 &amp;&amp; c &lt; n
            &amp;&amp; grid&#91;r]&#91;c]!=0;
    }
}

Time Complexity

M*N*3^G, We are only running recursion in 3 direction (except for the starting call), as previous cell is already visited and cannot be visited again, where G indicate gold cells

Space Complexity

O(G), G – Total golden cells in the grid.

As the DFS call can only go so far as to visit all connected Golden cells, it has O(G)O(G)O(G) space complexity.

Rate this post


Viewing all articles
Browse latest Browse all 10

Trending Articles