Instead of declaring cur array length of n size in space optimisation part, we can declare cur size in for loop of i, of ith size, as at starting the cur will be of size ith(n-2) only and at last it will be of size 1.
People Please read (for dummies like me): This is the first lecture from where I can directly write memoized code, if put my mind into it can write tabulated one too, also my code worked every single time. >> Started this series and found the first few videos okay like i was understanding them but then came lecture 6 and 7, Ninja training and the unique path grid. procrastinated on them for many days like 5-6, used to skip or watch them but could'nt retain them, so used to rewatch similarly. I then decided to get really into it, to understand in depth. Drew recursion trees for the problems gave 2-3 hours on those two videos (L7 and L6) and now how things work is in my brain I just translate what I think into code. TLDR: Just stick with it, give each video as much time as you need to understand, striver has made each video concise at god level such that every video is important as well as connected. Like in this video, I just had to think about how sub recursion will happen, heck didn't even think, it was soo intuitive that I just wrote code, submitted, got accepted.
Exactly same thing happened with me ... ninja problem gives you a sudden blow but then after that suddenly the need to write whole recursive them memoized was gone i could directlt write the space opti for until this vid
this was not for me i am a bigger idiot i procastinated for like 2 weeks ki bhaiya c++ mein padfhate hai but found out pseudo code is enough and also learnt how to write in c++ so mera yaha aake double faida hua !
There is a famous saying in our language "NUV DEVUDU SAAMI" (I feel you are the god of dsa teaching, coding ) Thank you man!! The man, The myth, The legend One piece STRIVER..👑
UNDERSTOOD ! This is the first dp problem, i did it from very scratch (from recursion) to space optimisation by myself .. Thank u ,for providing such next 🔥🔥 content .
Just after 4 days of watching dp videos, I was able to solve this question (Recursive, memorization and tabulation ) on my own in less than 6 mins, Feeling so Proud !! I can't thank Striver enough for providing such an amazing content, It's really next level.
In tabulation, inside nested loop, if we run loop for j from 0-->i, then also everything will be fine (as dp[i][j] is dependent on dp[i+1][j] and dp[i+1][j+1], so for row i, there is no dependency of dp[i][j] on it's left or right column, so it doesn't matter in what order we are filling the (i)th row)
before starting this series, dp was like a nightmare for me. I tried a lot of different sources, but there is no match of your content. thanks for such overwhelming efforts.
Rather than creating a n*n dp matrix for memoization, one can reduce the space usage by half if he/she creates a triangular matrix. Here's the code: vector dp; for(int i=0; i
Understood! I have gone through a transformation! transformation in the way I perceive DP. initially, it was the thing I avoided listening to, now I came here and solved these questions by myself first and then watched your explanations. Thanks a lot for helping me and many others of my kind.
Just after watching 4-5 videos of this playlist ,I felt so confident in Dynamic programming. Earlier I used to think of dynamic programming as something really difficult and tough to crack , but after watching just 5 videos of this playlist, I started loving this topic. The entire credit goes to this one person , STRIVER. Big fan of your teaching style , feeling so happy that I came across this channel. May you keep obliging students with your knowledge and teaching skills 🙌
First time ever that I am going to watch his lecture after coding the question out. First it seemed complicated but then when I used the three tips told by him it just came out of the flow. Started with recursion -> memoized code -> tabulated version and then space optimised it to O(n). Might not be so special to all, but this feels so good to reach optimal solution to an unsolved problem of dp. Now I am gonna watch this lecture just to review what I could have done better.
Note for JAVA folks, if you're trying O(N) space solution and not getting the right answer that is because of this line in front = cur; because in Java, simple assignment of one array to another makes a shallow copy of the assigned array viz. both the arrays get affected if there is any change happened in any one of the array. So the simple solution in java is: front = cur.clone(); // creates deep copy of the array in Java
Thank you, I wasn't getting the correct answer because of this. But in the previous problems(minimum path sum) , the array assignment was working fine without clone() method.
I've been a student of scaler academy, they also explain concepts very nicely but the only difference I've found is they at some point get lost in explaining so much that they go out of structure and sometimes it doesn't feels smooth to run by mind. You sticking to single framework of "1. index 2. do stuffs 3. min/max/optimize" and carrying the same translated version in DP is very comforting to cognate things. Best part is after watching DP lec 7, I solved problems of lec 8,9,10,11 by my own without watching the lecture within an avg time of 10min on each WITHOUT EVEN FORMULATING THE RECURSION FORMULA ON NOTEBOOK. Due to sticking to single framework, it becomes sooooo smooth to formulate the recursion formula within mind itself and I directly wrote down the tabulation code ❤
"understood" Thank You SO Much STRIVER for teaching such an amazing stuff in the most easy way. Frankly, I was afraid of DP before starting your DP playlist. But now, I solved this particular problem by myself starting from Recursion to Space Optimization.... Thank You Again 💚
First of all, I Appreciate your work man. Thanks for your hard work. There’s a doubt, what if in tabulation we move from (0,0) to (n,0)……(n,m), will it still be considered a bottom up solution?
No, you cannot go from 0,0 to n,0 or n,m beacuse you need previous for current to fill that is you need to come from bottom to top in order to calculate the current row !! Hope you understand
June 2022: I solve leetcode daily challenge everyday, June's challenges are focused on DP mostly, so i decided to follow along this series to help me understand DP concepts. I solved today's challenge by myself and came here to continue the series. Surprise surprise It's the same question, ThankYou for such good explanations.
I did tabulation in this way, from first row to last row.It was easier that discussed in video. #include int minimumPathSum(vector& triangle, int n){ // Write your code here. vector dp(n,vector(n,0));
Solution using one DP array only. Hey, striver. Really appreciate your efforts. Just because of you I feel much more confident in DP now. For this question, if we run the loop from 0 to
Isn't the problem statement wrong? (code studio link) It is: " if you are at index j in row i, then you can move to i or i + 1 index in row j + 1 in each step." Shouldn't it be: " if you are at index j in row i, then you can move to j or j + 1 index in row i + 1 in each step." So at next row, either at j, or j+1, right?
hey, striver, before watching this lecture I solved this using (n-1) to 0 recursion fashion, which has a time complexity O9=(n.2^n) because standing at last row, I need to call all the cells inside a for loop. yes, 0 to (n-1) should be preferable here, because before returning either in tabulation or memoization don't need to campare and return the minimum. but in my fashion I didn't get TLE in memoization, then how u got TLE in ur memoization because in both fashion T.C will be O(n^2) after applying memoization. and thank you very much, you are teaching me a lot :-)
first time i wrote full code myself just after understanding 2-3 min of video, i can't explain how much confident I am in dp. Have to say best dp playlist ever, thank you so much Raj Bhaiaya.
i did the space optimization from (0,0) to (m-1,n-1), for me it was easy to understand as in the previous lecture's also you used the same approach here the code for that in java(you can ask chatgpt to convert it into c or c++) public static int minimumPathSum(int[][] arr, int n) { int[] dp=new int[n]; dp[0]=arr[0][0]; int min=Integer.MAX_VALUE; for(int i=1;i
I think instead of two arrays, the space optimization can be done with a single array(dp) itself. Because we are allowed to go down and down-right, when we calculate ans for (i,j), there is no further use of (i+1,j). Hence we can directly put that calculated ans in dp[ j ]. Do let me know if I am wrong.
Nope that would manipulate the values in down and diagonal Lets say : 2 3 4 6 5 7 4 1 8 3 if you are changing the values in the same dp array i.e prev[j] then at i = 1 and j = 0 prev[j] or prev[1] would result in 10 due to min(10,13) but the result is 9. Try figuring out.
Thanks would be a very small word of appreciation for the incredible work you do . After watching dp vieos for 2 days, I could do the recursive approach to memoization and tabulation all , without watching your solution,THANK YOU!!
thank you , getting problems very well did submit it before watching video and then watching video for better concepts and methods like i didn't got the how to write the recursion for particular problem so i tried for DP and i did it and it made my day thank you again
I think it's the first time in my life that I am visiting an edu based channel on youtube everyday and reloading the uploads to find whether striver uploaded or scheduled a new video..."ANYONE CAN OPEN A CZcams CHANNEL AND TEACH , BUT ONLY A VERY FEW CAN CREATE AN IMPACT"..
bhaiya maine last ke 3 question khud se solve kr liyea bina solution deakhe yn yoh ab mai intelligent hogya hu(mushkil h) yn fir aapne padhaya bht acha hai jo b hai maza aarha hai😃
I am finally able to do recursion + memoization + tabulation with space optimization all by myself, thankyou so much for your teachings Striver ❤🔥❤🔥❤🔥❤🔥
i'm wondring that is it really me ? who was not able to approach any dp question until I started your dp series , and now I'm able to solve every problem before watching the solution video for that , Thanks man
Thanks Striver, was able to make it after watching just your 11 videos. class Solution { public int f(List triangle,int row,int idx,int [][] dp) { if(dp [row][idx] != -1) { return dp[row][idx]; } if(row == triangle.size()-2) { return dp[row][idx] = Math.min(triangle.get(row).get(idx)+triangle.get(row+1).get(idx),triangle.get(row).get(idx)+triangle.get(row+1).get(idx+1)); } int down = triangle.get(row).get(idx) + f(triangle,row+1,idx,dp); int dia = triangle.get(row).get(idx) + f(triangle,row+1,idx+1,dp); return dp[row][idx] = Math.min(down,dia); } public int minimumTotal(List triangle) { if(triangle.size() == 1) { return triangle.get(0).get(0); } int [][] dp = new int [triangle.size()][triangle.size()]; for(int [] i : dp) { Arrays.fill(i,-1); } return f(triangle,0,0,dp); } }
Without using a 'cur' reference. Space complexity: O(n) instead of O(2n). def minimumPathSum(triangle, n): # base case: last row dp = triangle[-1] for i in range(n - 2, -1, -1): for j in range(i + 1): dp[j] = triangle[i][j] + min(dp[j], dp[j + 1]) return dp[0]
In case if anyone wants to start from bottom can use below code. You will get correct answers for small cases but TLE for large case as Complexity is O(N^3). So it is better to start from i=0 as solved by Striver Sir. int go(int i,int j,vector &arr,vector &dp){ if(i==0 && j==0){ return arr[i][j]; } if(i
Understood sir 😁, i know there will be lots of aferts you are putting on each lecture of dp but i am waiting every day when new lecture will come, my telling me some time why not you upload all videos at a time😂 but it is not possible very well explanation sir👍
one correction to the solution given in the video initializing dp array to -1 may not work in every single case as given triangle can have negative elements also so having -1 @ any cell of dp does not mean that the solution is not computed already Hope it makes sense to you
Man awesome content did it by myself and yeah i did it by using nested loop for col from 0 to i instead of i to 0 like u did and it works so thanks again
we can reduce space and time complexity by this : int minimumTotal(vector& triangle) { int m = triangle.size(); vector dp = triangle[m-1]; for(int i = m-2; i>=0 ;i--) { for(int j = 0; j
Here in recursive and tabulation method the time complxity is O(n^2) in both only space complexity is less in tabulation method.As time complexity is same for both method why we are not getting TLE for tabulation method??
@22:05 it should be i-1 and j-1 resp as tabualtion is opposite of the rec approach sorry i get it Im wrong we are already traversing in opposite direction so whatever is there in video , is bloddy 100% correct man US!
Understood Striver... And by seeing this video I too optimized space rather than cur and front, we can use triangle and see this is working fine Hope you like it int minimumPathSum(vector& triangle, int n){ for(int i = n-2;i>=0;i--){ for(int j = i;j>=0;j--){ int d = triangle[i][j] + triangle[i+1][j]; int dg = triangle[i][j] + triangle[i+1][j+1];
int cur = min(d,dg); triangle[i][j] = cur; } } return triangle[0][0]; }
I need your support, and you can do that by giving me a like, and commenting "understood" if I was able to explain you.
Please correct the link of the problem
Understood Sir .
understood
Instead of declaring cur array length of n size in space optimisation part, we can declare cur size in for loop of i, of ith size, as at starting the cur will be of size ith(n-2) only and at last it will be of size 1.
pls hindi me vdo bana do hm lg ko smjhna h phle fir code krna h
People Please read (for dummies like me):
This is the first lecture from where I can directly write memoized code, if put my mind into it can write tabulated one too, also my code worked every single time.
>> Started this series and found the first few videos okay like i was understanding them but then came lecture 6 and 7, Ninja training and the unique path grid. procrastinated on them for many days like 5-6, used to skip or watch them but could'nt retain them, so used to rewatch similarly. I then decided to get really into it, to understand in depth. Drew recursion trees for the problems gave 2-3 hours on those two videos (L7 and L6) and now how things work is in my brain I just translate what I think into code.
TLDR: Just stick with it, give each video as much time as you need to understand, striver has made each video concise at god level such that every video is important as well as connected. Like in this video, I just had to think about how sub recursion will happen, heck didn't even think, it was soo intuitive that I just wrote code, submitted, got accepted.
same bro same
Exactly same thing happened with me ... ninja problem gives you a sudden blow but then after that suddenly the need to write whole recursive them memoized was gone i could directlt write the space opti for until this vid
this was not for me i am a bigger idiot i procastinated for like 2 weeks ki bhaiya c++ mein padfhate hai but found out pseudo code is enough and also learnt how to write in c++ so mera yaha aake double faida hua !
Same Bro Same
Us Bhai Us
There is a famous saying in our language
"NUV DEVUDU SAAMI" (I feel you are the god of dsa teaching, coding )
Thank you man!!
The man, The myth, The legend
One piece STRIVER..👑
Hi mama, ekkada nundi nuvvu?
@@tanmaisaichennagiri5543 chennai lo
Even after saying that he wont do the recursion tree , he did draw it ... Hats off to dedication and your motto to help every student
That space optimization is so godlike! This will surely impress interviewers! Thanks a lot striver bro
Even you will not require extra space other than given grid if you calculate maximym inplace
UNDERSTOOD !
This is the first dp problem, i did it from very scratch (from recursion) to space optimisation by myself ..
Thank u ,for providing such next 🔥🔥 content .
Just after 4 days of watching dp videos, I was able to solve this question (Recursive, memorization and tabulation ) on my own in less than 6 mins, Feeling so Proud !! I can't thank Striver enough for providing such an amazing content, It's really next level.
In tabulation, inside nested loop, if we run loop for j from 0-->i, then also everything will be fine (as dp[i][j] is dependent on dp[i+1][j] and dp[i+1][j+1], so for row i, there is no dependency of dp[i][j] on it's left or right column, so it doesn't matter in what order we are filling the (i)th row)
It doesn’t really matter in this case, I checked it that way too, it works.
Yeah 👍
I was also wondering same. Thanks for clarification 🎉
I wrote it that way 😊
Ya, even I got the same doubt. Thanks for clarifying 👍
before starting this series, dp was like a nightmare for me. I tried a lot of different sources, but there is no match of your content.
thanks for such overwhelming efforts.
Rather than creating a n*n dp matrix for memoization, one can reduce the space usage by half if he/she creates a triangular matrix. Here's the code:
vector dp;
for(int i=0; i
Yeah thats good but even though it would lead to (n*i) T.C which is >> O(n).
Understood!
I have gone through a transformation!
transformation in the way I perceive DP.
initially, it was the thing I avoided listening to, now I came here and solved these questions by myself first and then watched your explanations.
Thanks a lot for helping me and many others of my kind.
Just after watching 4-5 videos of this playlist ,I felt so confident in Dynamic programming. Earlier I used to think of dynamic programming as something really difficult and tough to crack , but after watching just 5 videos of this playlist, I started loving this topic. The entire credit goes to this one person , STRIVER.
Big fan of your teaching style , feeling so happy that I came across this channel.
May you keep obliging students with your knowledge and teaching skills 🙌
First time ever that I am going to watch his lecture after coding the question out. First it seemed complicated but then when I used the three tips told by him it just came out of the flow. Started with recursion -> memoized code -> tabulated version and then space optimised it to O(n).
Might not be so special to all, but this feels so good to reach optimal solution to an unsolved problem of dp. Now I am gonna watch this lecture just to review what I could have done better.
Note for JAVA folks, if you're trying O(N) space solution and not getting the right answer that is because of this line in front = cur; because in Java, simple assignment of one array to another makes a shallow copy of the assigned array viz. both the arrays get affected if there is any change happened in any one of the array. So the simple solution in java is:
front = cur.clone(); // creates deep copy of the array in Java
Thank you, I wasn't getting the correct answer because of this. But in the previous problems(minimum path sum) , the array assignment was working fine without clone() method.
You are a genius :) @SR
int[] dp1 = new int[tri.size()];
for(int i=0;i=0;i--) {
int[] newDp = new int[dp1.length];
for(int j=i;j>=0;j--) {
int down = tri.get(i).get(j) + dp1[j];
int dia = tri.get(i).get(j) + dp1[j+1];
newDp[j] = Math.min(down, dia);
}
dp1 = newDp;
}
return dp1[0];
If you initialize the newDp array within the for loop you do not need to do clone stuff.
Thank You so much bro
I've been a student of scaler academy, they also explain concepts very nicely but the only difference I've found is they at some point get lost in explaining so much that they go out of structure and sometimes it doesn't feels smooth to run by mind. You sticking to single framework of "1. index 2. do stuffs 3. min/max/optimize" and carrying the same translated version in DP is very comforting to cognate things. Best part is after watching DP lec 7, I solved problems of lec 8,9,10,11 by my own without watching the lecture within an avg time of 10min on each WITHOUT EVEN FORMULATING THE RECURSION FORMULA ON NOTEBOOK. Due to sticking to single framework, it becomes sooooo smooth to formulate the recursion formula within mind itself and I directly wrote down the tabulation code ❤
+1
I just did last 3 questions on my own .... Sky high confidence !! Thanks a lot Striver Guruji
"understood"
Thank You SO Much STRIVER for teaching such an amazing stuff in the most easy way.
Frankly, I was afraid of DP before starting your DP playlist. But now, I solved this particular problem by myself starting from Recursion to Space Optimization....
Thank You Again 💚
i was strugling to solve questions when I started the series, now I am able to understand and solve by myself. Thank You soo much brother.
hats off to this legend i am able to code/logic my own ... still saw his video to cross verify ... loved it bro
The way you explain the things soo deeply , the concepts are printed in my mind
Love from Bhubaneswar , Odisha :)
It is 2024 and you are the best tutor I have ever come across. Kudos to your work sir!!
First of all, I Appreciate your work man. Thanks for your hard work. There’s a doubt, what if in tabulation we move from (0,0) to (n,0)……(n,m), will it still be considered a bottom up solution?
Nah! How u move does not matters.
Recursion-> top down
Tabulation-> bottom up
No, you cannot go from 0,0 to n,0 or n,m beacuse you need previous for current to fill that is you need to come from bottom to top in order to calculate the current row !! Hope you understand
for new viewers: 16:45
Striver is really good at teaching DP. my friend suggested me his playlist and I'm really glad I took his suggestion.
Perfect explanation of how dp and space optimization can be done. such a Amazing video man.
🗿 Space optimization at the end is the thing that makes your dp videos God level 🔥
Just Stunning 💥
June 2022:
I solve leetcode daily challenge everyday, June's challenges are focused on DP mostly, so i decided to follow along this series to help me understand DP concepts. I solved today's challenge by myself and came here to continue the series. Surprise surprise It's the same question, ThankYou for such good explanations.
I did tabulation in this way, from first row to last row.It was easier that discussed in video.
#include
int minimumPathSum(vector& triangle, int n){
// Write your code here.
vector dp(n,vector(n,0));
for(int i=0;i
How is it any easier than what's discussed in the video
@@rahulchaudhary2441 It is because we are thinking of each and every possible case here, and not generalizing
@@himanshuagrawal8012 I think yes, if you choose to directly go for the tabulation method rather than going from intuitive recursive one.
Solution using one DP array only.
Hey, striver. Really appreciate your efforts.
Just because of you I feel much more confident in DP now.
For this question, if we run the loop from 0 to
Isn't the problem statement wrong? (code studio link)
It is: " if you are at index j in row i, then you can move to i or i + 1 index in row j + 1 in each step."
Shouldn't it be: " if you are at index j in row i, then you can move to j or j + 1 index in row i + 1 in each step."
So at next row, either at j, or j+1, right?
Yes yes it's wrong
I am able to think myself about the soultion till space optimization before watching your complete video. Thaks a lot. You made dp simple for me
hey, striver, before watching this lecture I solved this using (n-1) to 0 recursion fashion, which has a time complexity O9=(n.2^n) because standing at last row, I need to call all the cells inside a for loop.
yes, 0 to (n-1) should be preferable here, because before returning either in tabulation or memoization don't need to campare and return the minimum.
but in my fashion I didn't get TLE in memoization, then how u got TLE in ur memoization because in both fashion T.C will be O(n^2) after applying memoization.
and thank you very much, you are teaching me a lot :-)
yes you right we can go from n-1 to 0 as well you wont get TLE
first time i wrote full code myself just after understanding 2-3 min of video, i can't explain how much confident I am in dp. Have to say best dp playlist ever, thank you so much Raj Bhaiaya.
Now space optimisation seems so easy
Ty❣️
Bro how to calculate time complexity of recursive function
Im not able to think it..
Striver, you are a saviour for us. I was able to do this question by myself. Thanks a lot for providing such a valuable content :)
Understood.
Please do not reduce the time length. This is perfect. Please do not reduce quality.
i did the space optimization from (0,0) to (m-1,n-1), for me it was easy to understand as in the previous lecture's also you used the same approach
here the code for that in java(you can ask chatgpt to convert it into c or c++)
public static int minimumPathSum(int[][] arr, int n) {
int[] dp=new int[n];
dp[0]=arr[0][0];
int min=Integer.MAX_VALUE;
for(int i=1;i
Understood Sir .
I found that if someone has enough knowledge in Recursion ,it should be easy for them to learn Dynamic Programming. Right Sir?
Yes
I think instead of two arrays, the space optimization can be done with a single array(dp) itself. Because we are allowed to go down and down-right, when we calculate ans for (i,j), there is no further use of (i+1,j). Hence we can directly put that calculated ans in dp[ j ]. Do let me know if I am wrong.
Nope that would manipulate the values in down and diagonal
Lets say :
2
3 4
6 5 7
4 1 8 3
if you are changing the values in the same dp array i.e prev[j] then at i = 1 and j = 0 prev[j] or prev[1] would result in 10 due to min(10,13)
but the result is 9. Try figuring out.
Bro when iss cp sheet release pls replay bro
Khud se bhi kuch kr le
@@willturner3440 LoL
Amazing playlist for DP, understood each and every concept. Thank you very much💚
Thanks would be a very small word of appreciation for the incredible work you do .
After watching dp vieos for 2 days, I could do the recursive approach to memoization and tabulation all , without watching your solution,THANK YOU!!
thank you , getting problems very well
did submit it before watching video and then watching video for better concepts and methods
like i didn't got the how to write the recursion for particular problem so i tried for DP and i did it and it made my day
thank you again
Solved without watching this video. It was all thanks to your top class previous videos. Hats off to your teaching skills. Thank you so much.
I think it's the first time in my life that I am visiting an edu based channel on youtube everyday and reloading the uploads to find whether striver uploaded or scheduled a new video..."ANYONE CAN OPEN A CZcams CHANNEL AND TEACH , BUT ONLY A VERY FEW CAN CREATE AN IMPACT"..
Is it really me Solving dynamic problems with clear understanding and confidence or am I dreaming? Thanks Striver.
Understood Striverrr!!!! Especially the tabulation part was very insightful!
bhaiya maine last ke 3 question khud se solve kr liyea bina solution deakhe
yn yoh ab mai intelligent hogya hu(mushkil h) yn fir aapne padhaya bht acha hai
jo b hai maza aarha hai😃
Absolute Gem!!! COuld do it easily, without seeing your video, thanks for the concept build up.
i did this que by myself , thanks striver
2 mins into the video and I was able to solve this in 5 mins. Thanks to striver's unmatchable teaching technique
I am finally able to do recursion + memoization + tabulation with space optimization all by myself, thankyou so much for your teachings Striver ❤🔥❤🔥❤🔥❤🔥
i'm wondring that is it really me ? who was not able to approach any dp question until I started your dp series , and now I'm able to solve every problem before watching the solution video for that , Thanks man
Solved it myself without the video! I understand what I am looking at for this problem due to the previous problems having such a good explanations.
Thanks Striver, was able to make it after watching just your 11 videos.
class Solution {
public int f(List triangle,int row,int idx,int [][] dp)
{
if(dp [row][idx] != -1)
{
return dp[row][idx];
}
if(row == triangle.size()-2)
{
return dp[row][idx] = Math.min(triangle.get(row).get(idx)+triangle.get(row+1).get(idx),triangle.get(row).get(idx)+triangle.get(row+1).get(idx+1));
}
int down = triangle.get(row).get(idx) + f(triangle,row+1,idx,dp);
int dia = triangle.get(row).get(idx) + f(triangle,row+1,idx+1,dp);
return dp[row][idx] = Math.min(down,dia);
}
public int minimumTotal(List triangle) {
if(triangle.size() == 1)
{
return triangle.get(0).get(0);
}
int [][] dp = new int [triangle.size()][triangle.size()];
for(int [] i : dp)
{
Arrays.fill(i,-1);
}
return f(triangle,0,0,dp);
}
}
Man o Man....Understanding dp is no longer myth
when u don't need to see code after explanation of logic then u can assume watching one and only striver bhaiya k playlist 🥰🥰❤❤
yooooo i tried this on my own after listening to the logic and got all testcases passed... #STRIVEROP #STRIVERNATION
Best DP series on youtube bhaiya....
Very helpful.
Understood ! thanks for this amazing series...
Understood and fantasized with the beauty of problem Solving
understood sir! HATS OFF!
hats off to you!! lovely explanantion! no words🙏
Without using a 'cur' reference. Space complexity: O(n) instead of O(2n).
def minimumPathSum(triangle, n):
# base case: last row
dp = triangle[-1]
for i in range(n - 2, -1, -1):
for j in range(i + 1):
dp[j] = triangle[i][j] + min(dp[j], dp[j + 1])
return dp[0]
Huge amount of respect. Thank you Striver. "Understood".❤
understood completely, thank yo so much, I am genuinely excited while solving these problems now. Thank you
In case if anyone wants to start from bottom can use below code. You will get correct answers for small cases but TLE for large case as Complexity is O(N^3). So it is better to start from i=0 as solved by Striver Sir.
int go(int i,int j,vector &arr,vector &dp){
if(i==0 && j==0){
return arr[i][j];
}
if(i
#include
int f(int i,int j,vector& t,vector& dp){
if(i==0 && j==0){
return t[i][j];
}
if(ii || j
Understood sir 😁, i know there will be lots of aferts you are putting on each lecture of dp but i am waiting every day when new lecture will come, my telling me some time why not you upload all videos at a time😂 but it is not possible very well explanation sir👍
Yes every lecture takes a lot of time, you can see by the number of codes written.
great striver is being the one and only one
love the space optimization part it's so analytical
In the second loop we can do it by starting from j=0 to j
"UNDERSTOOD BHAIYA!!"
one correction to the solution given in the video
initializing dp array to -1 may not work in every single case as given triangle can have negative elements also
so having -1 @ any cell of dp does not mean that the solution is not computed already
Hope it makes sense to you
I used INT_MAX, but atleast the algorithm works correctly with -1 as well.
bro this is perfectly understand from point to point
can be done in constant space too :
int minimumTotal(vector& triangle) {
int n = triangle.size();
for(int i=1; i
Understood and I am able to solve this question myself
loved this amazing Technique of solving DP on Grids 💌💌
understood,
thankyou!
Thankyou striver sir
This first dp problem which I solved for recursion to space optimization all my self
understood.. thanks
Man awesome content
did it by myself
and yeah i did it by using nested loop for col from 0 to i instead of i to 0 like u did and it works
so thanks again
Never seen such space optimization in any videos wow amazing 😯
Understood!! I really, really, really appreciate for your explanation!!!
US. Thanks a lot! You are too good in teaching such tough topics.
Awesome content, a whole hearted "understood" from my side.
Understood sir ❤❤❤
this can be also done with single array as we dont need front one every time we will oveeride the value every time
thanks bro for developing thinking solved this question by myself
How the heck u make Space Optim so Smooth man................Hatsss Offff 🤩🤩
we can reduce space and time complexity by this :
int minimumTotal(vector& triangle) {
int m = triangle.size();
vector dp = triangle[m-1];
for(int i = m-2; i>=0 ;i--)
{
for(int j = 0; j
Here in recursive and tabulation method the time complxity is O(n^2) in both only space complexity is less in tabulation method.As time complexity is same for both method why we are not getting TLE for tabulation method??
31:12
in recursion solution at line 6 there is wrong condition.
dp[i][j] != -1
is wrong condition as we have range
-10^6
@22:05
it should be i-1 and j-1 resp as tabualtion is opposite of the rec approach
sorry
i get it
Im wrong
we are already traversing in opposite direction
so whatever is there in video , is bloddy 100% correct man
US!
Understood very very welllllll..
"Understood " thank you raj bhaiya !
Understood Striver...
And by seeing this video I too optimized space
rather than cur and front, we can use triangle and see this is working fine
Hope you like it
int minimumPathSum(vector& triangle, int n){
for(int i = n-2;i>=0;i--){
for(int j = i;j>=0;j--){
int d = triangle[i][j] + triangle[i+1][j];
int dg = triangle[i][j] + triangle[i+1][j+1];
int cur = min(d,dg);
triangle[i][j] = cur;
}
}
return triangle[0][0];
}
This question was asked in D.E Shaw in OA
UNDERSTOOD... !
Thanks striver for the video... :)
Thanks Striver. Understood Triangle problem.
Bottom up revusive sol- class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
n = len(triangle)
# for i in range(n):
# for j in range(0,i+1):
def calc(x,y):
if x == 0 and y ==0:
return triangle[0][0]
if x < 0 or y < 0 or y > x:
return float('inf')
up = calc(x-1,y)
up_left = calc(x-1,y-1)
return triangle[x][y] + min(up,up_left)
min_path = float('inf')
for i in range(n):
min_path = min(min_path,calc(n-1,i))
return min_path
Understood 💯💯 Great Explanation. Thank you very much for all you efforts🔥🔥
By far the best dp series 👌👌👌 Thank You soooo much ...