Skip to content

Commit 298e7be

Browse files
chore: add LeetCode daily solution
1 parent fdb6fbc commit 298e7be

5 files changed

Lines changed: 150 additions & 0 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Maximum Amount of Money Robot Can Earn (Medium)
2+
3+
**Problem ID:** 3418
4+
**Date:** 2026-04-02
5+
**Link:** https://leetcode.com/problems/maximum-amount-of-money-robot-can-earn/
6+
7+
## Approach
8+
9+
To solve the problem of maximizing the amount of money the robot can earn while navigating through the grid, we can use a dynamic programming approach.
10+
11+
### Main Idea:
12+
The key is to maintain a dynamic programming table that tracks the maximum coins the robot can collect at each cell while considering the possibility of neutralizing up to 2 robbers. We will utilize a 3D DP array where `dp[i][j][k]` represents the maximum coins collected when reaching cell `(i, j)` with `k` neutralizations used (where `k` can be 0, 1, or 2).
13+
14+
### Approach:
15+
1. **Initialization**: Start by initializing the DP table. The robot starts at `(0, 0)`, so set `dp[0][0][0]` to `coins[0][0]` if it's non-negative, or to `-inf` if it's negative (indicating an impossible state without neutralization).
16+
17+
2. **Filling the DP Table**:
18+
- Iterate through each cell `(i, j)` in the grid.
19+
- For each cell, calculate the maximum coins collectible from the top `(i-1, j)` and left `(i, j-1)` cells for each neutralization state `k`.
20+
- Depending on the value in `coins[i][j]`:
21+
- If it's positive, simply add it to the maximum coins from the previous cells.
22+
- If it's negative, check if neutralization is possible:
23+
- If `k` is 0, the robot cannot neutralize, so the value is subtracted from the total.
24+
- If `k` is 1 or 2, calculate the maximum coins by neutralizing the robbery (i.e., treat the negative value as 0 for that cell).
25+
26+
3. **Final Result**: The result will be the maximum value found in the last cell `(m-1, n-1)` across all neutralization states (0, 1, and 2).
27+
28+
### Data Structures:
29+
- A 3D array `dp[m][n][3]` to keep track of the maximum coins collected at each cell with different states of neutralization.
30+
31+
### Complexity:
32+
- **Time Complexity**: O(m * n), as we iterate through each cell and perform constant-time operations for each of the three neutralization states.
33+
- **Space Complexity**: O(m * n), for storing the DP table.
34+
35+
By following this structured approach, we ensure that we explore all potential paths the robot can take while maximizing the coins collected, effectively handling the constraints of robbers and neutralizations.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
class Solution {
2+
public int maxCoins(int[][] coins) {
3+
int m = coins.length, n = coins[0].length;
4+
int[][][] dp = new int[m][n][3];
5+
6+
for (int[][] layer : dp) {
7+
for (int[] row : layer) {
8+
Arrays.fill(row, Integer.MIN_VALUE);
9+
}
10+
}
11+
12+
dp[0][0][0] = coins[0][0] >= 0 ? coins[0][0] : 0;
13+
dp[0][0][1] = coins[0][0] < 0 ? -coins[0][0] : 0;
14+
dp[0][0][2] = 0;
15+
16+
for (int i = 0; i < m; i++) {
17+
for (int j = 0; j < n; j++) {
18+
for (int k = 0; k < 3; k++) {
19+
if (dp[i][j][k] == Integer.MIN_VALUE) continue;
20+
21+
int currentCoins = dp[i][j][k];
22+
if (i + 1 < m) {
23+
int nextCoins = currentCoins + coins[i + 1][j];
24+
if (coins[i + 1][j] < 0) {
25+
if (k < 2) {
26+
dp[i + 1][j][k + 1] = Math.max(dp[i + 1][j][k + 1], currentCoins);
27+
}
28+
} else {
29+
dp[i + 1][j][k] = Math.max(dp[i + 1][j][k], nextCoins);
30+
}
31+
}
32+
if (j + 1 < n) {
33+
int nextCoins = currentCoins + coins[i][j + 1];
34+
if (coins[i][j + 1] < 0) {
35+
if (k < 2) {
36+
dp[i][j + 1][k + 1] = Math.max(dp[i][j + 1][k + 1], currentCoins);
37+
}
38+
} else {
39+
dp[i][j + 1][k] = Math.max(dp[i][j + 1][k], nextCoins);
40+
}
41+
}
42+
}
43+
}
44+
}
45+
46+
int maxProfit = Integer.MIN_VALUE;
47+
for (int k = 0; k < 3; k++) {
48+
maxProfit = Math.max(maxProfit, dp[m - 1][n - 1][k]);
49+
}
50+
51+
return maxProfit;
52+
}
53+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
var maxCoins = function(coins) {
2+
const m = coins.length;
3+
const n = coins[0].length;
4+
const dp = Array.from({ length: m }, () => Array.from({ length: n }, () => Array(3).fill(-Infinity)));
5+
6+
dp[0][0][0] = coins[0][0] >= 0 ? coins[0][0] : 0;
7+
if (coins[0][0] < 0) dp[0][0][1] = -coins[0][0];
8+
if (coins[0][0] < 0) dp[0][0][2] = -coins[0][0];
9+
10+
for (let i = 0; i < m; i++) {
11+
for (let j = 0; j < n; j++) {
12+
for (let k = 0; k < 3; k++) {
13+
if (i === 0 && j === 0) continue;
14+
15+
const currentCoins = coins[i][j];
16+
const gain = currentCoins >= 0 ? currentCoins : 0;
17+
const loss = currentCoins < 0 ? -currentCoins : 0;
18+
19+
if (i > 0) {
20+
dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j][k] + gain);
21+
if (k > 0) {
22+
dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j][k - 1] + gain);
23+
}
24+
if (k > 0 && loss > 0) {
25+
dp[i][j][k] = Math.max(dp[i][j][k], dp[i - 1][j][k - 1]);
26+
}
27+
}
28+
if (j > 0) {
29+
dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j - 1][k] + gain);
30+
if (k > 0) {
31+
dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j - 1][k - 1] + gain);
32+
}
33+
if (k > 0 && loss > 0) {
34+
dp[i][j][k] = Math.max(dp[i][j][k], dp[i][j - 1][k - 1]);
35+
}
36+
}
37+
}
38+
}
39+
}
40+
41+
return Math.max(dp[m - 1][n - 1][0], dp[m - 1][n - 1][1], dp[m - 1][n - 1][2]);
42+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
def maxCoins(self, coins: List[List[int]]) -> int:
3+
m, n = len(coins), len(coins[0])
4+
dp = [[[-float('inf')] * 3 for _ in range(n)] for _ in range(m)]
5+
dp[0][0][0] = coins[0][0]
6+
7+
for i in range(m):
8+
for j in range(n):
9+
for k in range(3):
10+
if i > 0:
11+
dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k] + coins[i][j])
12+
if coins[i][j] < 0 and k > 0:
13+
dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k-1] + coins[i][j])
14+
if j > 0:
15+
dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k] + coins[i][j])
16+
if coins[i][j] < 0 and k > 0:
17+
dp[i][j][k] = max(dp[i][j][k], dp[i][j-1][k-1] + coins[i][j])
18+
19+
return max(dp[m-1][n-1])

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,3 +411,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
411411
- 2026-03-30 — [Check if Strings Can be Made Equal With Operations II](https://leetcode.com/problems/check-if-strings-can-be-made-equal-with-operations-ii/) (Medium) → `Medium/2026-03-30-2840-Check-if-Strings-Can-be-Made-Equal-With-Operations-II`
412412
- 2026-03-31 — [Lexicographically Smallest Generated String](https://leetcode.com/problems/lexicographically-smallest-generated-string/) (Hard) → `Hard/2026-03-31-3474-Lexicographically-Smallest-Generated-String`
413413
- 2026-04-01 — [Robot Collisions](https://leetcode.com/problems/robot-collisions/) (Hard) → `Hard/2026-04-01-2751-Robot-Collisions`
414+
- 2026-04-02 — [Maximum Amount of Money Robot Can Earn](https://leetcode.com/problems/maximum-amount-of-money-robot-can-earn/) (Medium) → `Medium/2026-04-02-3418-Maximum-Amount-of-Money-Robot-Can-Earn`

0 commit comments

Comments
 (0)