You are given a gold mine grid
of size m x n
, where 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.For example:
grid = [[0,6,0],[5,8,7],[0,9,0]]
The correct output is 24
because the optimal path to get the maximum gold is 9 -> 8 -> 7
.
Another example:
grid = [[1,0,7],[2,0,6],[3,4,5],[0,3,0],[9,0,20]]
In this case, the correct output is 28
. The optimal path is 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7
.
How would you approach this problem? Consider the constraints: 1 <= m, n <= 15
, and 0 <= grid[i][j] <= 100
. There are at most 25 cells containing gold.
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force method in this scenario involves exploring every possible path through the gold mine. We start at each possible location and try every possible route, collecting gold along the way. Ultimately, we'll pick the path that gives us the most gold.
Here's how the algorithm would work step-by-step:
def get_maximum_gold(grid):
rows = len(grid)
columns = len(grid[0])
maximum_total_gold = 0
def explore_path(row, column, current_gold, visited):
if row < 0 or row >= rows or column < 0 or column >= columns or grid[row][column] == 0 or (row, column) in visited:
return current_gold
current_gold += grid[row][column]
visited = set(visited)
visited.add((row, column))
# Explore all 4 possible directions
up = explore_path(row - 1, column, current_gold, visited)
down = explore_path(row + 1, column, current_gold, visited)
left = explore_path(row, column - 1, current_gold, visited)
right = explore_path(row, column + 1, current_gold, visited)
return max(up, down, left, right)
# Iterate through each cell in the grid
for row in range(rows):
for column in range(columns):
# Only start exploring from cells with gold
if grid[row][column] != 0:
maximum_total_gold = max(maximum_total_gold, explore_path(row, column, 0, set()))
# Start exploration from the current cell
return maximum_total_gold
The challenge is to find the most gold you can collect by walking through a grid. The best way to solve it is by exploring all possible paths from each starting point and remembering the best amount of gold collected from each location to avoid repeating work.
Here's how the algorithm would work step-by-step:
def get_maximum_gold(grid): rows = len(grid)
cols = len(grid[0])
max_gold_collected = 0
def explore_path(row, col, current_gold_total, visited):
nonlocal max_gold_collected
# Update maximum gold if current path is better.
max_gold_collected = max(max_gold_collected, current_gold_total)
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
for delta_row, delta_col in directions:
new_row = row + delta_row
new_col = col + delta_col
# Check boundaries and if cell is valid to explore.
if 0 <= new_row < rows and 0 <= new_col < cols and \
grid[new_row][new_col] != 0 and (new_row, new_col) not in visited:
# Recursively explore the path.
explore_path(new_row, new_col, current_gold_total + grid[new_row][new_col], visited | {(new_row, new_col)})
# Iterate to find a starting point to explore.
for row in range(rows):
for col in range(cols):
if grid[row][col] != 0:
# Start exploring from the current valid cell.
explore_path(row, col, grid[row][col], {(row, col)})
return max_gold_collected
Case | How to Handle |
---|---|
Null or empty grid | Return 0 immediately as there's no grid to traverse. |
Grid with single cell containing 0 | Return 0 as no gold can be collected from that cell. |
Grid with single cell containing positive gold | Return the cell's gold value since it is the maximum possible. |
Grid with all cells containing 0 | Return 0 as no gold can be collected. |
Grid with large dimensions (maximum size constraints) | Ensure the recursive calls do not exceed stack limits, potentially use iterative DFS instead for scalability. |
Grid contains high gold values, potentially causing integer overflow when summing the path | Use a larger data type (e.g., long) to store the path sum if necessary to prevent overflow. |
Disconnected graph where no path can reach all gold mines | The DFS approach explores all possible paths from each starting point so will find the optimal path even if disconnected. |
Multiple paths with the same maximum gold value exist | The algorithm finds one of the paths with maximum gold and returns that value, so it doesn't need to consider every maximum path. |