Path with Maximum Gold - Problem
Path with Maximum Gold

Imagine you're a treasure hunter exploring an ancient gold mine! You have a rectangular grid representing the mine, where each cell contains a certain amount of gold (or 0 if it's empty rock).

Your mission is to find the path that allows you to collect the maximum amount of gold possible. Here are the rules of your expedition:

๐Ÿƒ Movement: From any cell, you can move one step in any of the 4 directions (up, down, left, right)
๐Ÿ’ฐ Collection: You automatically collect all gold in each cell you visit
๐Ÿšซ No Revisiting: Once you've been to a cell, you can't return to it in the same path
โ›” No Empty Cells: You cannot step on cells with 0 gold
๐ŸŽฏ Flexible Start/End: You can begin and end your journey at any cell containing gold

Return the maximum amount of gold you can collect in a single expedition!

Example: In a 3ร—3 grid with gold values [[1,2,3],[0,2,0],[7,6,5]], you could start at cell (2,0) with value 7, then move to collect 6, then 2, then 3 for a total of 18 gold!

Input & Output

example_1.py โ€” Basic Grid
$ Input: grid = [[1,2,3],[0,2,0],[7,6,5]]
โ€บ Output: 18
๐Ÿ’ก Note: The optimal path starts at (2,0) with value 7, moves to (2,1) with value 6, then to (1,1) with value 2, and finally to (0,2) with value 3. Total: 7 + 6 + 2 + 3 = 18
example_2.py โ€” Single Row
$ Input: grid = [[1,2,3,4]]
โ€บ Output: 10
๐Ÿ’ก Note: We can traverse the entire row from left to right (or right to left) collecting all gold: 1 + 2 + 3 + 4 = 10
example_3.py โ€” All Zeros
$ Input: grid = [[0,0],[0,0]]
โ€บ Output: 0
๐Ÿ’ก Note: No cells contain gold, so we cannot collect anything. The maximum gold is 0.

Constraints

  • 1 โ‰ค m, n โ‰ค 15 (grid dimensions)
  • 0 โ‰ค grid[i][j] โ‰ค 100
  • Grid will have at least one cell with gold > 0

Visualization

Tap to expand
๐Ÿดโ€โ˜ ๏ธ Treasure Hunter's Gold Mine ๐Ÿดโ€โ˜ ๏ธAncient Gold Mine - Find the Richest Path!1gold2gold3gold๐Ÿ•ณ๏ธpit2gold๐Ÿ•ณ๏ธpit7gold6gold5goldSTART HEREFINISH๐ŸŽฏ Backtracking Strategy1. ๐Ÿ Try each gold cell as starting point2. ๐Ÿšถ Use DFS to explore all directions3. โŒ Mark visited cells (set to 0)4. ๐Ÿ’ฐ Collect gold along the path5. ๐Ÿ”„ Backtrack when stuck6. โœ… Restore cell values7. ๐Ÿ† Track maximum gold found๐Ÿ’Ž Optimal Path: 7โ†’6โ†’2โ†’3 = 18 Gold ๐Ÿ’Žโšก Time Complexity: O(4^(cells)) | Space: O(cells) for recursion
Understanding the Visualization
1
Survey the Mine
Look at all possible starting positions with gold
2
Pick Starting Point
Begin DFS from a cell with gold, mark it as visited
3
Explore Neighbors
Check all 4 directions for valid moves (gold > 0, not visited)
4
Go Deeper
Recursively explore the best neighboring cell
5
Hit Dead End
When no more moves possible, record the gold collected
6
Backtrack
Unmark current cell and try alternative paths
7
Find Maximum
Compare all path totals and return the maximum
Key Takeaway
๐ŸŽฏ Key Insight: This problem requires exploring all possible paths systematically. Backtracking allows us to 'undo' our moves and try alternatives, ensuring we find the path with maximum gold while respecting the constraint of not revisiting cells.
Asked in
Amazon 42 Google 35 Microsoft 28 Meta 18
35.2K Views
Medium-High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen