Tensorum
LeetCode 3418 · Grid DP

Maximum Amount of Money Robot Can Earn

LeetCode 3418 · Medium · solution, intuition & step-by-step walkthrough

State = (i, j, k neutralisations left); at each cell try 'neutralise' and 'don't'.

MediumArrayDynamic ProgrammingMatrix
7329 msruntime · beats 5%
646.5 MBmemory · beats 5%
1approach
5accepted runs · last 9 May 2025

This is how I solved Maximum Amount of Money Robot Can Earn (LeetCode 3418), a medium problem from the Grid DP pattern. Below: the intuition, the approach step by step, my accepted Python code, animated test cases and a line-by-line debugger.

Intuition

The robot only moves right or down, so every path is a choice at each cell. The extra wrinkle is a budget of two 'neutralise' moves that turn a negative cell into 0. Add that remaining budget to the state and the problem becomes an ordinary grid DP over (row, col, budget).

Approach

  1. 1

    dfs(i, j, k) = best coins collectable from cell (i, j) to the bottom-right with k neutralisations left.

  2. 2

    Out of the grid → −∞ so that path is never chosen.

  3. 3

    At the destination, neutralise it if it's negative and budget remains.

  4. 4

    Otherwise: without neutralising, take coins[i][j] plus the better of down/right with the same k; with neutralising (k > 0), take max(coins, 0) and recurse with k-1.

  5. 5

    Return the better option; lru_cache stores all m·n·3 states.

How I solved it

Recursion + memo (3D state)PythonO(m·n·k) time · O(m·n·k) space
from functools import lru_cache
class Solution:
    def maximumAmount(self, coins: List[List[int]]) -> int:
        m=len(coins)
        n=len(coins[0])
        #get max - either nutralize or not nutralize option
        @lru_cache(maxsize=None)
        def dfs(i,j,k):
            if i>=m or j>=n:
                return float("-inf")
            if i==m-1 and j==n-1:
                if coins[i][j]<0 and k>0:
                    return max(coins[i][j],0)
                else:
                    return coins[i][j]
                
            with_neutralize=max(coins[i][j],0)+max(dfs(i+1,j,k-1),dfs(i,j+1,k-1)) if k>0 else float("-inf")
            with_out_neutralize=coins[i][j]+max(dfs(i+1,j,k),dfs(i,j+1,k))
            return max(with_neutralize,with_out_neutralize)
        return dfs(0,0,2)
       
Loading the step-through…

Maximum Amount of Money Robot Can Earn — FAQ

What is the intuition behind Maximum Amount of Money Robot Can Earn?

The robot only moves right or down, so every path is a choice at each cell. The extra wrinkle is a budget of two 'neutralise' moves that turn a negative cell into 0. Add that remaining budget to the state and the problem becomes an ordinary grid DP over (row, col, budget).

How do you solve Maximum Amount of Money Robot Can Earn (LeetCode 3418)?

1. dfs(i, j, k) = best coins collectable from cell (i, j) to the bottom-right with k neutralisations left. 2. Out of the grid → −∞ so that path is never chosen. 3. At the destination, neutralise it if it's negative and budget remains. 4. Otherwise: without neutralising, take coins[i][j] plus the better of down/right with the same k; with neutralising (k > 0), take max(coins, 0) and recurse with k-1. 5. Return the better option; lru_cache stores all m·n·3 states.

What is the time and space complexity of Maximum Amount of Money Robot Can Earn?

Time: O(m · n · 3). Space: O(m · n · 3). (Accepted approach: Recursion + memo (3D state).)

What are common mistakes in Maximum Amount of Money Robot Can Earn?

Neutralising a positive cell wastes budget — max(coin, 0) keeps it harmless but the k-1 branch is still explored. Deep recursion on large grids is slow in Python; an iterative 3-layer table is faster.