You are given an m x n grid. A robot starts at the top-left corner of the grid (0, 0) and wants to reach the bottom-right corner (m - 1, n - 1). The robot can move either right or down at any point in time.
The grid contains a value coins[i][j] in each cell:
coins[i][j] >= 0, the robot gains that many coins.coins[i][j] < 0, the robot encounters a robber, and the robber steals the absolute value of coins[i][j] coins.The robot has a special ability to neutralize robbers in at most 2 cells on its path, preventing them from stealing coins in those cells.
Note: The robot's total coins can be negative.
Return the maximum profit the robot can gain on the route.
Example 1:
Input: coins = [[0,1,-1],[1,-2,3],[2,-3,4]]
Output: 8
Explanation:
An optimal path for maximum coins is:
(0, 0) with 0 coins (total coins = 0).(0, 1), gaining 1 coin (total coins = 0 + 1 = 1).(1, 1), where there's a robber stealing 2 coins. The robot uses one neutralization here, avoiding the robbery (total coins = 1).(1, 2), gaining 3 coins (total coins = 1 + 3 = 4).(2, 2), gaining 4 coins (total coins = 4 + 4 = 8).Example 2:
Input: coins = [[10,10,10],[10,10,10]]
Output: 40
Explanation:
An optimal path for maximum coins is:
(0, 0) with 10 coins (total coins = 10).(0, 1), gaining 10 coins (total coins = 10 + 10 = 20).(0, 2), gaining another 10 coins (total coins = 20 + 10 = 30).(1, 2), gaining the final 10 coins (total coins = 30 + 10 = 40).
Constraints:
m == coins.lengthn == coins[i].length1 <= m, n <= 500-1000 <= coins[i][j] <= 1000Problem Overview: You are given a grid where each cell represents money gained or lost when the robot enters it. The robot starts at the top-left and must reach the bottom-right moving only right or down. Some cells contain robbers (negative values). The robot can neutralize up to two robbers, preventing the loss. The goal is to compute the maximum amount of money the robot can collect along a valid path.
Approach 1: Brute Force DFS (Exponential Time)
Explore every possible path from (0,0) to (m-1,n-1) using depth‑first search. At each cell you decide whether to move right or down. If the value is negative and you still have robber neutralizations left, branch into two choices: take the loss or neutralize it. This generates up to three states per cell and quickly explodes in size. Time complexity is roughly O(2^(m+n)) in the worst case with O(m+n) recursion space, which is impractical for larger grids but useful to reason about the state transitions.
Approach 2: Memoized Search / Top-Down DP (O(m * n * 3))
The key observation is that many DFS states repeat. The robot's future earnings depend only on three values: the current row, the current column, and how many robber neutralizations remain. Cache results using a memo table dp[row][col][k], where k ranges from 0 to 2. From each state, recursively compute the best value from moving right or down. When the current cell contains a negative value and k > 0, you can either accept the loss or neutralize the robber and treat the cell value as zero. Store the maximum result in the memo table so the same state never recomputes.
This transforms the exponential search into a bounded dynamic programming problem. Each cell is evaluated for three possible neutralization states, producing O(m * n * 3) time complexity and the same O(m * n * 3) space complexity for memoization. The approach fits naturally with recursion and memo caching in languages like Python, Java, C++, Go, or TypeScript.
The solution models a classic grid path optimization problem from dynamic programming, extended with an extra decision dimension. Thinking of the grid as a state graph helps: each state branches to two neighbors while tracking remaining power-ups. This pattern appears in many matrix path problems and constrained optimization problems on arrays.
Recommended for interviews: Memoized DFS (top-down DP). Interviewers expect you to identify overlapping subproblems and introduce a state like (row, col, remainingNeutralizations). Explaining the brute-force recursion first demonstrates understanding of the path choices, then adding memoization shows strong dynamic programming instincts.
We design a function dfs(i, j, k), which represents the maximum amount of coins the robot can collect starting from (i, j) with k conversion opportunities left. The robot can only move right or down, so the value of dfs(i, j, k) depends only on dfs(i + 1, j, k) and dfs(i, j + 1, k).
i geq m or j geq n, it means the robot has moved out of the grid, so we return a very small value.i = m - 1 and j = n - 1, it means the robot has reached the bottom-right corner of the grid. If k > 0, the robot can choose to convert the bandit at the current position, so we return max(0, coins[i][j]). If k = 0, the robot cannot convert the bandit at the current position, so we return coins[i][j].coins[i][j] < 0, it means there is a bandit at the current position. If k > 0, the robot can choose to convert the bandit at the current position, so we return coins[i][j] + max(dfs(i + 1, j, k), dfs(i, j + 1, k)). If k = 0, the robot cannot convert the bandit at the current position, so we return coins[i][j] + max(dfs(i + 1, j, k), dfs(i, j + 1, k)).Based on the above analysis, we can write the code for memoized search.
The time complexity is O(m times n times k), and the space complexity is O(m times n times k). Here, m and n are the number of rows and columns of the 2D array coins, and k is the number of conversion opportunities, which is 3 in this problem.
Python
Java
C++
Go
TypeScript
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force DFS | O(2^(m+n)) | O(m+n) | Conceptual baseline to understand path exploration and decision branching |
| Memoized Search (Top-Down DP) | O(m * n * 3) | O(m * n * 3) | Optimal solution for grid path problems with limited special actions |
| Bottom-Up DP (3D Table) | O(m * n * 3) | O(m * n * 3) | Iterative alternative when recursion depth or stack limits are a concern |
3418. Maximum Amount of Money Robot Can Earn | DP on Grids | Top Down Approach • Aryan Mittal • 2,474 views views
Watch 9 more video solutions →Practice Maximum Amount of Money Robot Can Earn with our built-in code editor and test cases.
Practice on FleetCode