He explained in the video, the max rewardValue is 2000 and it can be taken only if x is 1999. So, in worst case when x is 1999, we include rewardValue 2000. Which equals 2000+1999=3999
I had written the same code but mine is giving memory limit exced int solve(int n,int x,vector r,vector &dp,int i) { if(i == n) return 0;
if(dp[i][x] != -1) return dp[i][x];
int take = 0;
if(r[i] > x) take = r[i] + solve(n,x+r[i],r,dp,i+1);
int ntake = solve(n,x,r,dp,i+1);
return dp[i][x] = max(take,ntake);
} int maxTotalReward(vector& r) { sort(r.begin(),r.end()); int ans = 0; int n = r.size(); vector dp(2001,vector (4001,-1)); ans = solve(n,0,r,dp,0); return ans;
Amazing!! Nice Explanation
Thank you
happy to see you after while.
Thank you 😊
Super explanation ❤❤
Thank you
awesome explanation within minutes
Thank you 😊
not understood why you take dp[2001][4001] instead of dp[2001][4000001] i know it will give tle but sum can go upto that ...
He explained in the video, the max rewardValue is 2000 and it can be taken only if x is 1999. So, in worst case when x is 1999, we include rewardValue 2000. Which equals 2000+1999=3999
I had written the same code but mine is giving memory limit exced
int solve(int n,int x,vector r,vector &dp,int i)
{
if(i == n)
return 0;
if(dp[i][x] != -1)
return dp[i][x];
int take = 0;
if(r[i] > x)
take = r[i] + solve(n,x+r[i],r,dp,i+1);
int ntake = solve(n,x,r,dp,i+1);
return dp[i][x] = max(take,ntake);
}
int maxTotalReward(vector& r) {
sort(r.begin(),r.end());
int ans = 0;
int n = r.size();
vector dp(2001,vector (4001,-1));
ans = solve(n,0,r,dp,0);
return ans;
}
keep it up bhaiya
Thank you 😊
Nice explanation
Thank you 😊
Improve your dp explanation.
bhug
Noted