Maximum Amount of Money Robot Can Earn - Problem

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:

  • If coins[i][j] >= 0, the robot gains that many coins.
  • If 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.

Input & Output

Example 1 — Basic Grid
$ Input: coins = [[1, -3], [2, -4]]
Output: 3
💡 Note: Robot path: (0,0) → (1,0) → (1,1). Collect 1 + 2 = 3 coins, then use shield to neutralize -4 robber. Total: 3 coins.
Example 2 — All Positive
$ Input: coins = [[1, 2], [3, 4]]
Output: 10
💡 Note: No robbers present. Best path: (0,0) → (0,1) → (1,1) gives 1 + 2 + 4 = 7, or (0,0) → (1,0) → (1,1) gives 1 + 3 + 4 = 8. Wait, let me recalculate: path (0,0) → (1,0) → (1,1) = 1 + 3 + 4 = 8.
Example 3 — Multiple Robbers
$ Input: coins = [[-1, -2], [-3, -4]]
Output: -4
💡 Note: All cells have robbers. Best strategy: use 2 shields on the least negative cells. Path (0,0) → (0,1) → (1,1): neutralize -1 and -2, take -4. Total: 0 + 0 + (-4) = -4.

Constraints

  • 1 ≤ m, n ≤ 50
  • -100 ≤ coins[i][j] ≤ 100
  • Robot can neutralize at most 2 robbers

Visualization

Tap to expand
INPUT GRID1-32-4CoinsRobbersRobot starts at (0,0)Goal: reach (1,1)Moves: right or downSpecial Power:🛡️ Neutralize up to🛡️ 2 robbers (shields)3D DP ALGORITHM1Create 3D table dp[i][j][shields]2Initialize destination cell values3Fill table from bottom-right4Handle shield usage decisionsState Transitions:dp[i][j][k] = max coins atposition (i,j) with k shieldsTry: use shield vs take penaltyRESULTMaximum Profit3 coinsOptimal Path:(0,0) → (1,0) → (1,1)Coins: 1 + 2 + (-4)Shield Strategy:🛡️ Neutralize -4 robberFinal: 1 + 2 + 0 = 3Time: O(m×n×3)Space: O(m×n×3)Key Insight:Track the maximum coins achievable at each position for every possible numberof shields remaining. This allows optimal shield usage decisions at each step.TutorialsPoint - Maximum Amount of Money Robot Can Earn | 3D Dynamic Programming
Asked in
Google 25 Amazon 18 Microsoft 15 Facebook 12
23.4K Views
Medium Frequency
~35 min Avg. Time
856 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