Tensorum
YouTubeகற்கத் தொடங்குங்கள்
LeetCode 3418 · Grid DP

Maximum Amount of Money Robot Can Earn

LeetCode 3418 · நடுத்தரம் · தீர்வு, intuition & படிப்படியான வாக்த்ரூ

State = (i, j, k neutralisations left); at each cell try 'neutralise' and 'don't'.

நடுத்தரம்ArrayDynamic ProgrammingMatrix
LeetCode இல் திற ↗

நான் 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. 1

    dfs(i, j, k) = best coins collectable from cell (i, j) to the bottom-right with k neutralisations left.

  2. 2

    Out of the grid → −∞ so that path is never chosen.

  3. 3

    At the destination, neutralise it if it's negative and budget remains.

  4. 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. 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.