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.