hey folks ,,(3rd question Maximum Total Damage With Spell Casting) whats the prob in code logic . we maintain prev take only if curr-prev>=3 . int solve2(const vector&p,int i,int prev, map&mp,vector&dp){ if(i==p.size())return 0; if(dp[i]!=-1)return dp[i]; int take=0; if(abs(prev-p[i])>=3 || prev==-1){ take = solve2(p,i+1,p[i],mp,dp)+mp[p[i]]*p[i]; } int nottake = solve2(p,i+1,prev,mp,dp); return dp[i]= max(take,nottake);
} long long maximumTotalDamage(vector& power) { mapmp; int n = power.size(); for(int i=0;i
I just read your code. The problem here is that you have defined your Dp[level] as the maximum power you can make from this level to the end. But lets take a example like 1,6,7. Here your code will take 1 , 6 and when it will go to level 3(which is power 7). Take condition will not work and it will process to not take this 7 power element. But notice again your dp[level=3] will store 0 as the maximum spell it can create from this level. Which is wrong. So to prevent this from happening we will skip all the states in which take will not work and goes to the state in which take will work (if no exist we will return 0). We are skipping all the states because there is no point in doing so. Even if we go in those states we cant take so nothing will be added in our solution
@@vivekgupta3484 actually the way I implemented my each range representative node contain below info peakCount, leftEle, secondLeftEle, rightEle, secondRightEle using these I implemented :p, may sound dumb/way too complicated but in this direction only I got the intuition
Prefix sum and binary search in the homework question
I solved 3rd problem using dp and binary search
Finally coded last problem after learning about ordered set . Loved the approach !!
This is the tempo.
hey folks ,,(3rd question Maximum Total Damage With Spell Casting) whats the prob in code logic . we maintain prev take only if curr-prev>=3 . int solve2(const vector&p,int i,int prev, map&mp,vector&dp){
if(i==p.size())return 0;
if(dp[i]!=-1)return dp[i];
int take=0;
if(abs(prev-p[i])>=3 || prev==-1){
take = solve2(p,i+1,p[i],mp,dp)+mp[p[i]]*p[i];
}
int nottake = solve2(p,i+1,prev,mp,dp);
return dp[i]= max(take,nottake);
}
long long maximumTotalDamage(vector& power) {
mapmp;
int n = power.size();
for(int i=0;i
u forget to memorize the prev because index,prev varies according to states.
@@rahulyela ya true ,, but then range of prev would be max of element(1e9) so will get mle . so this approach will fail. anything else can be done?
@@noobchess2516 You don't have to include the previous element as dp state. Take it as a function variable
I just read your code. The problem here is that you have defined your Dp[level] as the maximum power you can make from this level to the end.
But lets take a example like 1,6,7. Here your code will take 1 , 6 and when it will go to level 3(which is power 7). Take condition will not work and it will process to not take this 7 power element. But notice again your dp[level=3] will store 0 as the maximum spell it can create from this level. Which is wrong.
So to prevent this from happening we will skip all the states in which take will not work and goes to the state in which take will work (if no exist we will return 0). We are skipping all the states because there is no point in doing so. Even if we go in those states we cant take so nothing will be added in our solution
@@blitztour5924 got it 👍 crazy explaination thanks
Solved last problem using Segment Tree, was difficult implementing custom segment tree node considering edge cases but made it
Why custom? wasn't it just maintaining 0/1 and range sum.
@@vivekgupta3484 actually the way I implemented my each range representative node contain below info
peakCount, leftEle, secondLeftEle, rightEle, secondRightEle
using these I implemented :p, may sound dumb/way too complicated but in this direction only I got the intuition
Bhaiya is "cracking the coding interview " book is worth to buy in 2024???
hi