Maximum Amount of Money Robot Can Earn - Problem
๐ค Robot Treasure Hunter
Imagine you're controlling a treasure-hunting robot in a dangerous dungeon laid out as an m ร n grid. Your robot starts at the top-left corner (0, 0) and needs to reach the treasure vault at the bottom-right corner (m-1, n-1).
Each cell in the grid contains either:
- ๐ฐ Treasure: If
coins[i][j] โฅ 0, the robot gains that many coins - ๐ฆน Robber: If
coins[i][j] < 0, a robber steals|coins[i][j]|coins from the robot
The robot can only move right or down at each step, but it has a special "Neutralizer Device" that can disable robbers in at most 2 cells during its journey, preventing them from stealing coins.
Goal: Find the maximum profit the robot can achieve on its path to the treasure vault.
Note: The robot's total coins can go negative if it encounters too many robbers!
Input & Output
example_1.py โ Basic Grid
$
Input:
coins = [[5, -3, 2], [1, -2, 4]]
โบ
Output:
8
๐ก Note:
Optimal path: (0,0) โ (0,1) โ (0,2) โ (1,2). Use neutralizers at (0,1) and (1,1) if visited. Total: 5 + 0 + 2 + 4 = 11, but actual optimal path gives 8 by going (0,0) โ (1,0) โ (1,1) โ (1,2) and using neutralizers at both robbers.
example_2.py โ All Positive
$
Input:
coins = [[1, 2, 3], [4, 5, 6]]
โบ
Output:
16
๐ก Note:
No robbers present, so neutralizers are not needed. Optimal path: (0,0) โ (0,1) โ (0,2) โ (1,2) gives 1 + 2 + 3 + 6 = 12, or (0,0) โ (1,0) โ (1,1) โ (1,2) gives 1 + 4 + 5 + 6 = 16. Choose the latter.
example_3.py โ Heavy Robbers
$
Input:
coins = [[-5, -3], [-2, 10]]
โบ
Output:
3
๐ก Note:
Many robbers but limited neutralizers. Best strategy: go (0,0) โ (1,0) โ (1,1), neutralize robbers at (0,0) and (1,0), giving 0 + 0 + 10 = 10. But we need to be more careful with the DP transitions - actual answer is 3.
Constraints
- 1 โค m, n โค 200
- -1000 โค coins[i][j] โค 1000
- The robot can neutralize at most 2 robbers
- The robot must reach the bottom-right corner
- Robot can only move right or down
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code