Skip to content

Commit 1992243

Browse files
Sync LeetCode submission Runtime - 516 ms (63.10%), Memory - 275.9 MB (44.52%)
1 parent 895c3c9 commit 1992243

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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] &gt;= 0</code>, the robot gains that many coins.</li>
7+
<li>If <code>coins[i][j] &lt; 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&#39;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>&nbsp;</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&#39;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>&nbsp;</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 &lt;= m, n &lt;= 500</code></li>
63+
<li><code>-1000 &lt;= coins[i][j] &lt;= 1000</code></li>
64+
</ul>
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
int maximumAmount(vector<vector<int>>& coins) {
4+
int n = coins.size();
5+
int m = coins[0].size();
6+
vector<vector<vector<int>>> dp(n, vector<vector<int>>(m, vector<int>(3, -1e9)));
7+
dp[0][0][0] = coins[0][0];
8+
dp[0][0][1] = 0;
9+
for (int i=0; i<n; i++) {
10+
for (int j=0; j<m; j++) {
11+
if (i > 0) {
12+
dp[i][j][0] = max(dp[i][j][0], dp[i-1][j][0] + coins[i][j]);
13+
14+
dp[i][j][1] = max(dp[i][j][1], dp[i-1][j][1] + coins[i][j]);
15+
dp[i][j][1] = max(dp[i][j][1], dp[i-1][j][0]);
16+
17+
dp[i][j][2] = max(dp[i][j][2], dp[i-1][j][2] + coins[i][j]);
18+
dp[i][j][2] = max(dp[i][j][2], dp[i-1][j][1]);
19+
}
20+
if (j > 0) {
21+
dp[i][j][0] = max(dp[i][j][0], dp[i][j-1][0] + coins[i][j]);
22+
23+
dp[i][j][1] = max(dp[i][j][1], dp[i][j-1][1] + coins[i][j]);
24+
dp[i][j][1] = max(dp[i][j][1], dp[i][j-1][0]);
25+
26+
dp[i][j][2] = max(dp[i][j][2], dp[i][j-1][2] + coins[i][j]);
27+
dp[i][j][2] = max(dp[i][j][2], dp[i][j-1][1]);
28+
}
29+
}
30+
}
31+
return *max_element(dp[n-1][m-1].begin(), dp[n-1][m-1].end());
32+
}
33+
};

0 commit comments

Comments
 (0)