Memoize Code || Best Solution
IntuitionApproachComplexity
Time complexity:

Space complexity:
Code
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->

# Approach
<!-- Describe your approach to solving the problem. -->

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->

# Code
```java []
class Solution {
    static Integer dp[][][];// imp Integer dp lena tha becz negative value is also present; so we cant take int dp and intialize with -1
    public static int solve(int i,int j,int k,int coins[][]){
        if(i>=coins.length || j>=coins[0].length)return Integer.MIN_VALUE;
        if(i==coins.length-1 && j==coins[0].length-1){
            if(coins[i][j]<0 && k>0)return 0;
            return coins[i][j];
        }
        if(dp[i][j][k]!=null)return dp[i][j][k];
        
       int take=Integer.MIN_VALUE,nottake=Integer.MIN_VALUE;
       take=coins[i][j]+Math.max(solve(i+1,j,k,coins),solve(i,j+1,k,coins));
       if(coins[i][j]<0 && k>0)nottake=Math.max(solve(i+1,j,k-1,coins),solve(i,j+1,k-1,coins));
       return dp[i][j][k]=Math.max(take,nottake);
       
    }
    public int maximumAmount(int[][] coins) {
        int n=coins.length;
        int m=coins[0].length;
         dp=new Integer[n][m][3];
     
        return solve(0,0,2,coins);
    }
}

['Java']