Maximum Amount of Money Robot Can Earn - Complete Solution Guide
Maximum Amount of Money Robot Can Earn is LeetCode problem 3418, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
You are given an m x n grid. A robot starts at the top-left corner of the grid (0, 0) and wants to reach the bottom-right corner (m - 1, n - 1) . The robot can move either right or down at any point in time. The grid contains a value coins[i][j] in each cell: If coins[i][j] >= 0 , the robot gains that many coins. If coins[i][j] < 0 , the robot encounters a robber, and the robber steals the absolute value of coins[i][j] coins. The robot has a special ability to neutralize robbers in at most 2 cel
Detailed Explanation
The problem presents a robot navigating a grid from the top-left to the bottom-right corner, moving only right or down. Each cell in the grid contains a value representing coins gained (positive) or coins stolen by a robber (negative). The robot can neutralize the robbers in at most two cells along its path. The goal is to find the maximum total coins the robot can have at the end of its journey.
Solution Approach
The solution uses dynamic programming with space optimization. It maintains two rows, `dp_prev` and `dp_curr`, to store the maximum coins for each cell, considering the number of neutralizations used so far (0, 1, or 2). For each cell, it checks if the cell contains a coin or a robber. If it's a coin, the robot gains the coin. If it's a robber, the robot can either neutralize the robber (if neutralizations are available) or have the coins stolen. The maximum coins for the current cell are calculated based on the maximum coins from the previous row and the previous column, considering both scenarios: neutralizing the robber or not.
Step-by-Step Algorithm
- Step 1: Initialize `dp_prev` with negative infinity, representing an unreachable state, except for the starting cell (0,0).
- Step 2: If the starting cell (0,0) has positive coins, initialize dp_prev[0][0] to that coin value. If it has negative coins, initialize dp_prev[0][0] to the coin value and dp_prev[0][1] to 0 (representing using one neutralization).
- Step 3: Iterate through the grid row by row, starting from the second row.
- Step 4: For each cell, calculate `dp_curr` based on `dp_prev` and the value of the current cell (coin or robber).
- Step 5: If the current cell contains a coin (positive value), update `dp_curr` with the maximum value obtained from the top or left cell in the previous row, plus the current cell's value.
- Step 6: If the current cell contains a robber (negative value), calculate two possible values: either neutralize the robber (if neutralization is available) or take the loss. Update `dp_curr` with the maximum value of these two scenarios.
- Step 7: After each row is processed, update `dp_prev` with the calculated `dp_curr` to prepare for the next row.
- Step 8: After processing the entire grid, the maximum coin value among dp_prev[n-1][0], dp_prev[n-1][1], and dp_prev[n-1][2] represents the solution. Return the solution.
Key Insights
- Insight 1: Dynamic Programming is suitable because the optimal path to a cell depends on the optimal paths to the cells above and to the left.
- Insight 2: A 3D DP table is necessary to track the maximum coins at each cell for 0, 1, and 2 robber neutralizations used.
- Insight 3: Since the robot can only move right or down, the current state only depends on the previous row and the previous column, which enables space optimization using only two rows of the DP table.
Complexity Analysis
Time Complexity: O(m*n)
Space Complexity: O(n)
Topics
This problem involves: Array, Dynamic Programming, Matrix.
Companies
Asked at: PhonePe.