-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1219. Path with Maximum Gold #258
Comments
The "Path with Maximum Gold" problem is a typical backtracking problem where we need to find the maximum amount of gold we can collect in a grid-based mine. The challenge is to explore different paths under certain conditions like not revisiting any cell and avoiding cells that contain 0 gold. This problem requires recursive exploration of possible paths, which is where the backtracking approach comes in. Key Points
Approach
Plan
Let's implement this solution in PHP: 1219. Path with Maximum Gold <?php
/**
* @param Integer[][] $grid
* @return Integer
*/
function getMaximumGold($grid) {
$m = count($grid);
$n = count($grid[0]);
$dfs = function ($i, $j) use (&$dfs, $m, $n, &$grid) {
if ($i < 0 || $i >= $m || $j < 0 || $j >= $n || !$grid[$i][$j]) {
return 0;
}
$v = $grid[$i][$j];
$grid[$i][$j] = 0;
$ans = $v + max([$dfs($i - 1, $j), $dfs($i + 1, $j), $dfs($i, $j - 1), $dfs($i, $j + 1)]);
$grid[$i][$j] = $v;
return $ans;
};
$ans = 0;
for ($i = 0; $i < $m; ++$i) {
for ($j = 0; $j < $n; ++$j) {
$ans = max($ans, $dfs($i, $j));
}
}
return $ans;
}
// Example usage:
$grid1 = [[0,6,0],[5,8,7],[0,9,0]];
$grid2 = grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]];
echo getMaximumGold($grid1) . "\n"; // Output: 24
echo getMaximumGold($grid2) . "\n"; // Output: 28
?> Explanation:The DFS function works as follows:
Example WalkthroughExample 1: grid = [
[0,6,0],
[5,8,7],
[0,9,0]
]
Example 2: grid = [
[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]
]
Time ComplexityThe time complexity is O(m * n), where
Output for ExampleFor the example inputs:
grid = [
[0,6,0],
[5,8,7],
[0,9,0]
] Output:
grid = [
[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]
] Output: The "Path with Maximum Gold" problem is a great exercise in using DFS with backtracking. It requires careful exploration of all possible paths in a grid while ensuring we do not revisit cells. The approach demonstrated here ensures we can find the maximum amount of gold efficiently by using recursion and backtracking. The time complexity of O(m * n) makes the solution feasible even for the largest grid sizes, within the given constraints. |
Discussed in #257
Originally posted by mah-shamim August 9, 2024
Topics:
Array
,Backtracking
,Matrix
In a gold mine
grid
of sizem 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:
0
gold.Example 1:
[[0,6,0],
[5,8,7],
[0,9,0]]
Path to get the maximum gold, 9 -> 8 -> 7.
Example 2:
[[1,0,7],
[2,0,6],
[3,4,5],
[0,3,0],
[9,0,20]]
Path to get the maximum gold, 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7.
Constraints:
m == grid.length
n == grid[i].length
1 <= m, n <= 15
0 <= grid[i][j] <= 100
Hint:
The text was updated successfully, but these errors were encountered: