Maximum Amount of Money Robot Can Earn
LeetCode 3418 · मध्यम · समाधान, intuition और कदम-दर-कदम वॉकथ्रू
State = (i, j, k neutralisations left); at each cell try 'neutralise' and 'don't'.
मैंने Maximum Amount of Money Robot Can Earn (LeetCode 3418) ऐसे हल किया — Grid DP पैटर्न का एक मध्यम प्रॉब्लम। नीचे: intuition, कदम-दर-कदम approach, मेरा accepted Python कोड, animated test cases और line-by-line debugger।
नीचे का विवरण अंग्रेज़ी में है।
Intuition
The robot only moves right or down, so every path is a choice at each cell. The extra wrinkle is a budget of two 'neutralise' moves that turn a negative cell into 0. Add that remaining budget to the state and the problem becomes an ordinary grid DP over (row, col, budget).
Approach
- 1
dfs(i, j, k) = best coins collectable from cell (i, j) to the bottom-right with k neutralisations left.
- 2
Out of the grid → −∞ so that path is never chosen.
- 3
At the destination, neutralise it if it's negative and budget remains.
- 4
Otherwise: without neutralising, take coins[i][j] plus the better of down/right with the same k; with neutralising (k > 0), take max(coins, 0) and recurse with k-1.
- 5
Return the better option; lru_cache stores all m·n·3 states.
मैंने कैसे हल किया
Recursion + memo (3D state)PythonO(m·n·k) time · O(m·n·k) space
from functools import lru_cache
class Solution:
def maximumAmount(self, coins: List[List[int]]) -> int:
m=len(coins)
n=len(coins[0])
#get max - either nutralize or not nutralize option
@lru_cache(maxsize=None)
def dfs(i,j,k):
if i>=m or j>=n:
return float("-inf")
if i==m-1 and j==n-1:
if coins[i][j]<0 and k>0:
return max(coins[i][j],0)
else:
return coins[i][j]
with_neutralize=max(coins[i][j],0)+max(dfs(i+1,j,k-1),dfs(i,j+1,k-1)) if k>0 else float("-inf")
with_out_neutralize=coins[i][j]+max(dfs(i+1,j,k),dfs(i,j+1,k))
return max(with_neutralize,with_out_neutralize)
return dfs(0,0,2)
Maximum Amount of Money Robot Can Earn — अक्सर पूछे जाने वाले प्रश्न
Maximum Amount of Money Robot Can Earn के पीछे intuition क्या है?
The robot only moves right or down, so every path is a choice at each cell. The extra wrinkle is a budget of two 'neutralise' moves that turn a negative cell into 0. Add that remaining budget to the state and the problem becomes an ordinary grid DP over (row, col, budget).
Maximum Amount of Money Robot Can Earn (LeetCode 3418) कैसे हल करें?
1. dfs(i, j, k) = best coins collectable from cell (i, j) to the bottom-right with k neutralisations left. 2. Out of the grid → −∞ so that path is never chosen. 3. At the destination, neutralise it if it's negative and budget remains. 4. Otherwise: without neutralising, take coins[i][j] plus the better of down/right with the same k; with neutralising (k > 0), take max(coins, 0) and recurse with k-1. 5. Return the better option; lru_cache stores all m·n·3 states.
Maximum Amount of Money Robot Can Earn की time और space complexity क्या है?
Time: O(m · n · 3)। Space: O(m · n · 3)। (Accepted approach: Recursion + memo (3D state)।)
Maximum Amount of Money Robot Can Earn में आम गलतियाँ क्या हैं?
Neutralising a positive cell wastes budget — max(coin, 0) keeps it harmless but the k-1 branch is still explored. Deep recursion on large grids is slow in Python; an iterative 3-layer table is faster.