3418. Maximum Amount of Money Robot Can Earn
Approach
3D DP where dp[i][j][k] = max coins at cell (i,j) with k neutralizations remaining.
At each cell, either collect the coin value normally or use a neutralization (skip negative cells).
Transition from top and left neighbours. Answer is max over all k at bottom-right cell.
Key Techniques
Array problems involve manipulating elements stored in a contiguous block of memory. Key techniques include two-pointer traversal, prefix sums, sliding windows, and in-place partitioning. In C#, arrays are zero-indexed and fixed in size — use List<T> when you need dynamic resizing.
Dynamic programming solves problems by breaking them into overlapping sub-problems and storing results to avoid redundant work. The key steps are: define state, write a recurrence relation, set base cases, and choose top-down (memoization) or bottom-up (tabulation). DP often yields O(n²) → O(n) time improvements over brute force.
Matrix problems often involve BFS/DFS flood fill, dynamic programming on 2D grids, or spiral/diagonal traversal. For row × column DP, break it into 1D sub-problems column by column. Common pitfalls: boundary checks and modifying the input matrix in-place.
// Approach: 3D DP where dp[i][j][k] = max coins at cell (i,j) with k neutralizations remaining.
// At each cell, either collect the coin value normally or use a neutralization (skip negative cells).
// Transition from top and left neighbours. Answer is max over all k at bottom-right cell.
// Time: O(m * n) Space: O(m * n)
public class Solution
{
public int MaximumAmount(int[][] coins)
{
int m = coins.Length;
int n = coins[0].Length;
// dp[i][j][k] := the maximum profit at position (i, j) with k remaining neutralizations
int[,,] dp = new int[m, n, 4];
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < 4; k++)
dp[i, j, k] = int.MinValue / 2;
}
}
// Base case: the robot starts at the top-left corner.
dp[0, 0, 2] = coins[0][0];
if (coins[0][0] < 0)
dp[0, 0, 1] = 0; // Neutralize the robber.
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
for (int k = 0; k < 3; k++)
{
if (i > 0)
dp[i, j, k] = Math.Max(dp[i, j, k], Math.Max(dp[i - 1, j, k] + coins[i][j], dp[i - 1, j, k + 1]));
if (j > 0)
dp[i, j, k] = Math.Max(dp[i, j, k], Math.Max(dp[i, j - 1, k] + coins[i][j], dp[i, j - 1, k + 1]));
}
}
}
int max = int.MinValue;
for (int k = 0; k < 4; k++)
max = Math.Max(max, dp[m - 1, n - 1, k]);
return max;
}
}