Reddit Experience · Apr 2026

Silly doubt in today's potd

3 upvotes 5 replies

Interview Experience

I tried to solve it using the 2D dp approach by taking row no. and col nom as my states but I got the wrong answer.Whn I looked into the solution it said we need to take lives left as the third state.

Full Details

I tried to solve it using the 2D dp approach by taking row no. and col nom as my states but I got the wrong answer.Whn I looked into the solution it said we need to take lives left as the third state.I am not able to understand why so .Can someone explain why lives left should be taken as the third state For reference this was my approach: class Solution { public: int solve(vector<vector<int>>& coins,vector<vector<int>>& dp,int live,int i ,int j){ int n = coins.size(); int m = coins[0].size(); if(i>=n||j>=m){return INT_MIN;} if(i==n-1&&j==m-1){ if(coins[i][j]>=0){return dp[i][j]=coins[i][j];} else{ if(live>0){return dp[i][j]=0;} else{ dp[i][j]=coins[i][j]; } } } if(dp[i][j]!=INT_MIN){return dp[i][j];} if(coins[i][j]>=0){return dp[i][j]=coins[i][j]+max(solve(coins,dp,live,i+1,j),solve(coins,dp,live,i,j+1));} else{ if(live>0){return dp[i][j]=max(solve(coins,dp,live-1,i+1,j),solve(coins,dp,live-1,i,j+1));} else{ dp[i][j]=coins[i][j]+max(solve(coins,dp,live,i+1,j),solve(coins,dp,live,i,j+1)); } } return INT_MIN; } int maximumAmount(vector<vector<int>>& coins) { int n = coins.size(); int m = coins[0].size(); vector<vector<int>>dp(n,vector<int>(m,INT_MIN)); return solve(coins,dp,2,0,0); } };

Free preview. Unlock all questions →

Topics

Dynamic Programming