|
| 1 | +<p>You are given an <code>m x n</code> grid. A robot starts at the top-left corner of the grid <code>(0, 0)</code> and wants to reach the bottom-right corner <code>(m - 1, n - 1)</code>. The robot can move either right or down at any point in time.</p> |
| 2 | + |
| 3 | +<p>The grid contains a value <code>coins[i][j]</code> in each cell:</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>If <code>coins[i][j] >= 0</code>, the robot gains that many coins.</li> |
| 7 | + <li>If <code>coins[i][j] < 0</code>, the robot encounters a robber, and the robber steals the <strong>absolute</strong> value of <code>coins[i][j]</code> coins.</li> |
| 8 | +</ul> |
| 9 | + |
| 10 | +<p>The robot has a special ability to <strong>neutralize robbers</strong> in at most <strong>2 cells</strong> on its path, preventing them from stealing coins in those cells.</p> |
| 11 | + |
| 12 | +<p><strong>Note:</strong> The robot's total coins can be negative.</p> |
| 13 | + |
| 14 | +<p>Return the <strong>maximum</strong> profit the robot can gain on the route.</p> |
| 15 | + |
| 16 | +<p> </p> |
| 17 | +<p><strong class="example">Example 1:</strong></p> |
| 18 | + |
| 19 | +<div class="example-block"> |
| 20 | +<p><strong>Input:</strong> <span class="example-io">coins = [[0,1,-1],[1,-2,3],[2,-3,4]]</span></p> |
| 21 | + |
| 22 | +<p><strong>Output:</strong> <span class="example-io">8</span></p> |
| 23 | + |
| 24 | +<p><strong>Explanation:</strong></p> |
| 25 | + |
| 26 | +<p>An optimal path for maximum coins is:</p> |
| 27 | + |
| 28 | +<ol> |
| 29 | + <li>Start at <code>(0, 0)</code> with <code>0</code> coins (total coins = <code>0</code>).</li> |
| 30 | + <li>Move to <code>(0, 1)</code>, gaining <code>1</code> coin (total coins = <code>0 + 1 = 1</code>).</li> |
| 31 | + <li>Move to <code>(1, 1)</code>, where there's a robber stealing <code>2</code> coins. The robot uses one neutralization here, avoiding the robbery (total coins = <code>1</code>).</li> |
| 32 | + <li>Move to <code>(1, 2)</code>, gaining <code>3</code> coins (total coins = <code>1 + 3 = 4</code>).</li> |
| 33 | + <li>Move to <code>(2, 2)</code>, gaining <code>4</code> coins (total coins = <code>4 + 4 = 8</code>).</li> |
| 34 | +</ol> |
| 35 | +</div> |
| 36 | + |
| 37 | +<p><strong class="example">Example 2:</strong></p> |
| 38 | + |
| 39 | +<div class="example-block"> |
| 40 | +<p><strong>Input:</strong> <span class="example-io">coins = [[10,10,10],[10,10,10]]</span></p> |
| 41 | + |
| 42 | +<p><strong>Output:</strong> <span class="example-io">40</span></p> |
| 43 | + |
| 44 | +<p><strong>Explanation:</strong></p> |
| 45 | + |
| 46 | +<p>An optimal path for maximum coins is:</p> |
| 47 | + |
| 48 | +<ol> |
| 49 | + <li>Start at <code>(0, 0)</code> with <code>10</code> coins (total coins = <code>10</code>).</li> |
| 50 | + <li>Move to <code>(0, 1)</code>, gaining <code>10</code> coins (total coins = <code>10 + 10 = 20</code>).</li> |
| 51 | + <li>Move to <code>(0, 2)</code>, gaining another <code>10</code> coins (total coins = <code>20 + 10 = 30</code>).</li> |
| 52 | + <li>Move to <code>(1, 2)</code>, gaining the final <code>10</code> coins (total coins = <code>30 + 10 = 40</code>).</li> |
| 53 | +</ol> |
| 54 | +</div> |
| 55 | + |
| 56 | +<p> </p> |
| 57 | +<p><strong>Constraints:</strong></p> |
| 58 | + |
| 59 | +<ul> |
| 60 | + <li><code>m == coins.length</code></li> |
| 61 | + <li><code>n == coins[i].length</code></li> |
| 62 | + <li><code>1 <= m, n <= 500</code></li> |
| 63 | + <li><code>-1000 <= coins[i][j] <= 1000</code></li> |
| 64 | +</ul> |
0 commit comments