Leetcode 1219: Path with Maximum Gold

grid47
grid47
Exploring patterns and algorithms
Jul 8, 2024 7 min read

In a gold mine grid of size m x n, each cell contains an integer representing the amount of gold in that cell. A cell with 0 means no gold is present. The task is to find the maximum amount of gold that can be collected by following certain movement rules: You can move one step in any of the four directions (left, right, up, down), but you cannot visit the same cell twice, and you cannot move to a cell that contains 0 gold.
Problem
Approach
Steps
Complexity
Input: You are given a 2D grid where each cell contains an integer representing the amount of gold. A value of 0 means no gold is present in the cell.
Example: Input: grid = [[0, 6, 0], [5, 8, 7], [0, 9, 0]]
Constraints:
• 1 <= m, n <= 15
• 0 <= grid[i][j] <= 100
• There are at most 25 cells containing gold.
Output: Return the maximum amount of gold that can be collected under the given movement constraints.
Example: Output: 24
Constraints:
Goal: To find the maximum amount of gold that can be collected, we need to explore the grid and recursively calculate the maximum gold collected from each valid starting point.
Steps:
• Traverse each cell in the grid. If the cell contains gold, recursively explore all four directions (up, down, left, right).
• Track the gold collected along the path and ensure that you don't revisit cells or step on a cell with 0 gold.
• Return the maximum gold collected from any starting cell.
Goal: The grid size is constrained to 15 x 15, and there are at most 25 cells with gold.
Steps:
• 1 <= m, n <= 15
• 0 <= grid[i][j] <= 100
• There are at most 25 cells containing gold.
Assumptions:
• The grid will always contain at least one cell with gold.
• The grid size will not exceed 15 x 15.
Input: Input: grid = [[0, 6, 0], [5, 8, 7], [0, 9, 0]]
Explanation: The path to collect the maximum gold is 9 → 8 → 7, collecting a total of 24 units of gold.

Input: Input: grid = [[1, 0, 7], [2, 0, 6], [3, 4, 5], [0, 3, 0], [9, 0, 20]]
Explanation: The path to collect the maximum gold is 1 → 2 → 3 → 4 → 5 → 6 → 7, collecting a total of 28 units of gold.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus