Skip to content
Decoder

Path with Maximum Gold

Medium

Problem

A gold mine is a grid where grid[r][c] is the amount of gold in that cell (0 means empty). You collect gold by walking a path: start at any cell with gold, and each step moves up, down, left or right to another cell with gold. You may never visit a cell twice and never step onto an empty cell. Return the maximum gold you can collect.

Example 1

[[0, 6, 0],

Example 2

 [5, 8, 7],

Example 3

Input
[0, 9, 0]]
Output
24
Why
9 → 8 → 7

Example 4

[[1, 0, 7],

Example 5

 [2, 0, 6],

Example 6

 [3, 4, 5],

Example 7

 [0, 3, 0],

Example 8

Input
[9, 0, 20]]
Output
28
Why
1 → 2 → 3 → 4 → 5 → 6 → 7

Constraints

  • 1 <= rows, cols <= 15
  • 0 <= grid[r][c] <= 100
  • At most 25 cells contain gold.

Read it once before you touch anything

Want some pressure? Start the timer. In an interview you get about two minutes to understand the problem.

  1. 1Read the goalWhat goes in, what comes out
  2. 2Scan the examplesCheck one by hand
  3. 3Note the constraintsThey set your Big O
Say what it asks in one sentence
Say it out loud first, then compare with ours.