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
Asked in
25.0K Views
Medium Frequency
~15 min Avg. Time
850 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