Climbing Stairs (LeetCode 70) | Full solution with animations | Dynamic Easy | Study Algorithms
HTML-код
- Опубликовано: 30 июл 2024
- To see more videos like this, you can buy me a coffee: www.buymeacoffee.com/studyalg...
Given a staircase with n steps, we need to find the total number of distinct ways to climb it by taking 1 or 2 steps at a time. Sure, this can be done by a brute force method and finding all the possibilities, but there is a really easy way to understand this. This video shows how you can form building blocks and ultimately arrive at a very efficient solution. To make things easy, there is also a dry run of code in JAVA.
Chapters:
00:00 - Intro
01:36 - Problem statement and description
04:51 - Brute Force Method
07:28 - How to understand and attack
09:41 - Finding an efficient solution
12:19 - Dry-run of Code
14:45 - Final Thoughts
Actual problem on LeetCode: leetcode.com/problems/climbin...
📚 Links to topics I talk about in the video:
Dynamic Programming: • Dynamic Programming ea...
Brute Force Solution: • Brute Force algorithms...
What is Big O?: • Big O Notation Simplif...
📘 A text based explanation is available at: studyalgorithms.com
Code on Github: github.com/nikoo28/java-solut...
Test-cases on Github: github.com/nikoo28/java-solut...
📖 Reference Books:
Starting Learn to Code: amzn.to/36pU0JO
Favorite book to understand algorithms: amzn.to/39w3YLS
Favorite book for data structures: amzn.to/3oAVBTk
Get started for interview preparation: amzn.to/39ysbkJ
🔗 To see more videos like this, you can show your support on: www.buymeacoffee.com/studyalg...
🎥 My Recording Gear:
Recording Light: amzn.to/3pAqh8O
Microphone: amzn.to/2MCX7qU
Recording Camera: amzn.to/3alg9Ky
Tablet to sketch and draw: amzn.to/3pM6Bi4
Surface Pen: amzn.to/3pv6tTs
Laptop to edit videos: amzn.to/2LYpMqn
💻 Get Social 💻
Follow on Facebook at: / studyalgos
Follow on Twitter at: / studyalgorithms
Follow on Tumblr at: / studyalgos
Subscribe to RSS feeds: studyalgorithms.com/feed/
Join fan mail: eepurl.com/g9Dadv
#leetcode #programming #interview
I have watched NeetCode, CS Dojo and Kevin Naughton's explanation but couldn't understand the concept behind arr[target] = arr[target-1] + arr[target-2] for this problem until I came across your "How to understand and attack" segment. Thank you for explaining like I'm 5.
I had the same issue. He explained it so much better!
I am just seeing this..I love the explanation...lols....
yes you right
Handsdown the best explanation available on youtube for leetcode 70!
Great video! Loved the visuals. You did an excellent job at breaking down the steps needed to approach this problem. Keep it up!
glad you liked it. If you want to see more content like this, consider joining my channel: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin
Provides sound explanation of algorithmic approach and solution, indeed worthy channel. Thanks.
Wowww. Thanks a million bro. After watching many turotials on this, yours is the best! I've understood it! Thanks
Thank you so much, you helped me understand dynamic programming with such a simple explanation and example.
Really excellent explanation, the breakdown of the problem made it much more easier to understand.
This was really helpful!!
Keep up the good work..
Thank you soo much this is my 1st dp question I'm glad i found good description of it thank a lot you are a great tutor
I read one of comment which was recently posted, but u replied to that comment also,great !!!
Man great breakdown. I'm doing interview prep by grinding on leetcode and your videos have been a great way for me to understand how to solve set problems. You do a great job of talking slowly but not too slow and breaking the problem down that is simple to understand.
same here
Dude, you nailed it. LOVE YOU!
Glad you enjoyed it 😄
I tried to solve this problem using recursion but TLE.... And you just gave me best solution .... Thank you so much guru🔥💯❤️🙏
Your excitement for teaching has no bounds 🙏
thanks for your kind words
Thanks for your simple explanation.
Thank you so much... Best and Easiest explanation ever .
Omg such an amazing explanation, thank you so much sir 🙏❤️
such a lovely solution and explanation
i find it much easier to understand these dsa problems when they are reduced to some math equivalent like at 11:49 Thank you so much
Great explaination!!
Great explanation.Thank you very much
thank you for the great explanation!
Very good way to teach brother very depth understanding of the question very good
Man great breakdown of this problem, simply the best!
You teach amazing. Thanks for making us understand difficult concepts in a simple way🙂
Glad you feel this way :)
Good one, bro. Thank you.
hats off to you sir, excellent explanation, and thnks a lot
Great explanation! This is my first DP, Memoisation Problem!
you are gonna love dynamic programming. Check out House Robber as well. ruclips.net/video/VXqUQYGMnQg/видео.html
If this guy makes a math course it’ll be one of the best.
true..and everyone will start coding.
your explanation skill is too good
Hey Nikhil, I really liked your way of explanation. keep making such videos. Subscribed.
Thanks for the sub!
well explained thanks you!!
Really well explained.
The way you explained it made it so easy to understand. Thank you, my friend, for helping me in my first dynamic programming question. Love from Haryana.
You're very welcome!
it's Amazing explanation . I loved it.❤❤❤❤❤❤❤❤❤❤❤❤
Thank you very much, sir.
I will say just fantastic!
Adbhoot. Love your explanation
great content!
just starting my leetCode journey, with no math background, this video helped me so much, thanks a lot!
I wish you all the very best :)
@@nikoo28 thanks!
Awesome explanation
awesome explaination bhaiya
Great video ❤
Yours was the best explanation.
Lit 🔥very well explained
You too are awesome 😎
The best explanation i have come across, i have watched other videos of same problem from other instructors , their explanation was also good, but Nikhil the way you've explained is so simple and top notch, thank you very much
really glad you feel that way 😄
very nice method of explanation....
Thank you sir!!
wow I learn now what is dp and memorization thank you sir
Thank you so much sir🎉
Great explanation
you simplified it to a level that my dumb brain can understand. Thank you.
Your Explanation is very good.
Glad you think so!
great man...!!!!
Thank you so much, first time while I saw the problem, i was like WTH how I am gonna solve this. After seeing your video it's so easy!! ❣
Love it!!! 😁
Thank you 😊😊
Boss you are a king of coding and the best teacher
Thank you so much
Best explanation behind the thought process of approaching the problem. I think we can reduce the space complexity to O(1) because we just need to return the value for the number of ways to reach the nth step. So a sliding window approach can also work, which is kinda similar to dynamic programming, but you don't memoize all values, just the previous 2 values and keep updating it.
Excellent
I wish you could have been my teacher for dsa! You are a true teacher, this was so simple to follow I want to cry. Thank you so much sir! Please keep making this type of videos they are life savers for people like me! Thank you thank you! Please could you touch upon recursion and tree problems please !
Thank you so much for your kind words. I do have videos on recursion and tree problems. Check out my playlist on algorithmic paradigms. :)
I hope they are helpful too.
@@nikoo28 why array is of n+1 size, why cant n??
Bcz index starts at 0 if you want to calculate the 8 step you need ans of 8th index but if u pick array of size n you would be providing ans of 7th index instead of 8@@santoshibora1892
Awesome Explanation bro
Thank you so much 🙂
Thanks my bro
just an amazing explanation
Glad you think so!
I was skeptical but in the end you delivered the understanding
glad i could help you
Thank you so much!
You're welcome!
thank u..
observing the output we can also use simple fib program
Bro u r really a bro ....
very nice explanantion baat to hai..
thanks very much for explain dp 🤗
Thank you so much 😊
you make me fall in love with dynamic programming .please do a live talk with us and give some advices for interview . thanks for the video nikhil.
glad you liked the video. Check out my latest video on Edit Distance too. Just uploaded it today :)
I will have a youtube live coming up soon.
@@nikoo28 glad to know. We like to talk with you.
Nice explanation.
Keep watching
Nice
Bahut badhiya padhate ho sir aap 🙏
thank you so so much
This can be done using 2 ints as well.
```golang
func climbStairs(n int) int {
if (n == 1 || n == 2) {
return n
}
a, b := 1, 1
for i := 2; i < n; i++ {
a, b = b, b + a
}
return a + b
}
```
Great video, what tool are you using with your pen to make those bright red lines?
that is an application called GoodNotes 6
the way of explaining was very very good. but we can use simple Fibonacci approach as well.
but how would you know that it is a fibonacci sequence??
This problem can also be done with the help of recursion
Really helpfull sir
The way you're explaining is >>>>>>>>>>>>>>>>>>>>>>>>> any other course or youtube channel
It will be so helpful if you tell about my channel to your friends/colleagues as well 😃
@@nikoo28 yes sir shared with community groups
why +1 i didn't get it ? isn't it like prefix sum? so we are basically storing sum of last two ele so why we need n+1 size instead of n ?
that is just to avoid index out of bounds exception
So, its a modified question of finonacci series problem
not exactly...it just happens to have a fibonacci pattern.
Is it bad to use recursive solutions for dynamic programming? I simply used recursion similar to fibonacci and then to make it more performant used a memo obj. That way it will avoid recursion for already encountered inputs. The tabulation approach seems lot less intuitive for more complex problems.
I won't say bad to use recursion but it is definitely less intuitive. When something goes wrong it is very hard to trace what happened, and fixing it becomes problematic. Recursive solutions look small for sure, but it is not necessary that they take less space as well.
Isnt this is tabulation rather than memoization? As we following bottom up approach from base case to final result 13:37
by tabulation if you mean storing all results in a table, then yes it is the same. Memoization is just a concept to store your calculated results, you do it in any data structure you like as long as you are saving space/time.
but if the no of stairs is 1 your code will cause indexoutofboundexception. In order to avoid that you should initialize 0 index value and 1 index value to 1 and run the loop starting from i=2;
You can reduce space complexity to O(1). It's a fibonnaci series in other name👍
But you need to understand the problem before you can derive the fibonacci series
op
Sir please make a series of all the 150 neet code questions using java, it's a humble request 🥺
Will upload soon
@@nikoo28 thank you ☺️
Min Cost Climbing Stairs also make vedio on these quation
let me find it
Sir please make whole series on Tree,Graph, DP plz sir
Hi, check my playlists on the channel.
Currently I am trying to add as many videos as I can…but just limited by the time I have available :)
@@nikoo2805 Thanks sir no problem
The complete playlist on graphs is now available: ruclips.net/p/PLFdAYMIVJQHNFJQt2eWA9Sx3R5eF32WPn
On How to understand and attack, how can (climb 1 stair from step 6 + climb 2 stairs from step 5) be translated to (number of ways to reach step 6 + number of ways to reach step 5)? HOW?
that is because you have to find the total number of ways.
if you are able to reach step 6, then with 1 more stair you will reach step 7.
so if you can reach step 6 in 10 ways, you can reach step 7 also by just climbing one more step. so all 10 ways you reached step 6 and then 1 more stair.
so you are reaching step 7 in 10 ways.
similarly, if you reach step 5 in 15 ways, then with just 2 more stairs you will reach step 7. so you can also reach step 7 in 15 ways.
total ways = number of ways to reach step 6 + number of ways to reach step 5
Thank you @@nikoo28
this feels exactly like Fibonacci sequence
Yes, but you need to arrive at that conclusion first
Confused.
Isn't this LeetCode 70 (Climbing Stairs) and not 47 (Permutations II)?
Thanks..this has been fixed :)
so solution of this problem is basically a fibbonacci?
you are absolutely correct. Watch another fun video on fibonacci series: ruclips.net/video/WuMTCaM2pk8/видео.html
I understood how to implement the code and I used recursion, like this:-
public class Solution{
public int climbStairs(int n) {
return fib(n);
}
public int fib(int n){
if(n
That will have a LOT of redundant method calls, but you can keep the recursive approach if you use memoization. Look that up.
There's also videos explaining this solution with that approach.
@@brunosdm Oh! I looked up memoization and watched the explanation by hackerrank. Thanks!
absolutely correct, with memoization you can store your results..so you are not computing again and again.
11:44 n minus 1 and n minus 2 😅😅
why array size is n+1
you need to start from 0
@@nikoo28 but you are not using 0th index right
Thank you bhai...apke tasveer print karke main kal se pooja karunga
thanks for the lovely feedback 🙏
I still didn't understand
This is just a fibonacci series answer.
but you need to reach over there first :)
@@nikoo28 Love your videos sir❤
you should make a udemy course and make $$
Haha..what course do you think I should make?
@@nikoo28 Udemy course on solving leetcode and another course on DSA
sub done 🤙