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 ofcoins[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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code