Climbing Stairs (LeetCode 70) | Full solution with animations | Dynamic Easy | Study Algorithms

  Рет қаралды 35,881

Nikhil Lohia

Nikhil Lohia

Күн бұрын

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

Пікірлер: 159
@pcccmn
@pcccmn 2 жыл бұрын
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.
@Arcadencebeats
@Arcadencebeats Жыл бұрын
I had the same issue. He explained it so much better!
@edoreemmanuel4250
@edoreemmanuel4250 7 ай бұрын
I am just seeing this..I love the explanation...lols....
@testing-js8iu
@testing-js8iu 4 ай бұрын
yes you right
@horcruxone
@horcruxone 2 ай бұрын
Handsdown the best explanation available on youtube for leetcode 70!
@Ranjan-xc5nl
@Ranjan-xc5nl 28 күн бұрын
Provides sound explanation of algorithmic approach and solution, indeed worthy channel. Thanks.
@dirtbag1126
@dirtbag1126 10 ай бұрын
Great video! Loved the visuals. You did an excellent job at breaking down the steps needed to approach this problem. Keep it up!
@nikoo28
@nikoo28 9 ай бұрын
glad you liked it. If you want to see more content like this, consider joining my channel: kzbin.info/door/T-S2ngqEBoYCM5UKuNeELgjoin
@jamjam3448
@jamjam3448 2 ай бұрын
Wowww. Thanks a million bro. After watching many turotials on this, yours is the best! I've understood it! Thanks
@niktri6805
@niktri6805 15 күн бұрын
Really excellent explanation, the breakdown of the problem made it much more easier to understand.
@vichitrakalakaar
@vichitrakalakaar 2 жыл бұрын
I tried to solve this problem using recursion but TLE.... And you just gave me best solution .... Thank you so much guru🔥💯❤️🙏
@yousefnegmeldin5903
@yousefnegmeldin5903 Ай бұрын
Thank you so much, you helped me understand dynamic programming with such a simple explanation and example.
@sa35085nhs
@sa35085nhs 5 ай бұрын
I read one of comment which was recently posted, but u replied to that comment also,great !!!
@zb2747
@zb2747 Жыл бұрын
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.
@priyagupta5139
@priyagupta5139 Ай бұрын
same here
@srikarjonnala6509
@srikarjonnala6509 10 күн бұрын
such a lovely solution and explanation
@purnachandrasahu622
@purnachandrasahu622 5 ай бұрын
This was really helpful!! Keep up the good work..
@kunalchauhan5294
@kunalchauhan5294 5 ай бұрын
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
@fourieruddin871
@fourieruddin871 5 ай бұрын
Your excitement for teaching has no bounds 🙏
@nikoo28
@nikoo28 4 ай бұрын
thanks for your kind words
@ajaygunalan1995
@ajaygunalan1995 Жыл бұрын
Dude, you nailed it. LOVE YOU!
@nikoo28
@nikoo28 Жыл бұрын
Glad you enjoyed it 😄
@susmitobhattacharyya1668
@susmitobhattacharyya1668 7 ай бұрын
Thanks for your simple explanation.
@user-wg6nk7ix7x
@user-wg6nk7ix7x 6 ай бұрын
Man great breakdown of this problem, simply the best!
@arjunb9342
@arjunb9342 Жыл бұрын
Thank you so much... Best and Easiest explanation ever .
@dailyprograming9003
@dailyprograming9003 10 ай бұрын
your explanation skill is too good
@nerd6134
@nerd6134 2 жыл бұрын
If this guy makes a math course it’ll be one of the best.
@ankithareddy4497
@ankithareddy4497 5 ай бұрын
true..and everyone will start coding.
@sukanyaghosh24
@sukanyaghosh24 9 ай бұрын
Omg such an amazing explanation, thank you so much sir 🙏❤️
@kasifmansuri7552
@kasifmansuri7552 10 ай бұрын
Great explaination!!
@tiyashasen5020
@tiyashasen5020 Жыл бұрын
You teach amazing. Thanks for making us understand difficult concepts in a simple way🙂
@nikoo28
@nikoo28 Жыл бұрын
Glad you feel this way :)
@vasavi5908
@vasavi5908 9 ай бұрын
Great explanation.Thank you very much
@vsh-torch
@vsh-torch 2 жыл бұрын
Good one, bro. Thank you.
@ParmodYadav
@ParmodYadav 5 ай бұрын
Very good way to teach brother very depth understanding of the question very good
@rubylnic
@rubylnic 11 ай бұрын
thank you for the great explanation!
@gauravkumar-ek8mr
@gauravkumar-ek8mr 2 жыл бұрын
Adbhoot. Love your explanation
@shortme9374
@shortme9374 2 ай бұрын
Hey Nikhil, I really liked your way of explanation. keep making such videos. Subscribed.
@nikoo28
@nikoo28 2 ай бұрын
Thanks for the sub!
@kavinsanthosh1306
@kavinsanthosh1306 Жыл бұрын
Great explanation! This is my first DP, Memoisation Problem!
@nikoo28
@nikoo28 Жыл бұрын
you are gonna love dynamic programming. Check out House Robber as well. kzbin.info/www/bejne/jInUhoSPfLKhh8k
@ram_mandir_india
@ram_mandir_india 7 ай бұрын
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.
@nikoo28
@nikoo28 6 ай бұрын
You're very welcome!
@_Adil.Khan_
@_Adil.Khan_ 2 жыл бұрын
I will say just fantastic!
@minecraftredstoneinspiration
@minecraftredstoneinspiration Жыл бұрын
Really well explained.
@Rajendrachoudhary-ut8ll
@Rajendrachoudhary-ut8ll 2 ай бұрын
Awesome explanation
@yashthakur5712
@yashthakur5712 6 ай бұрын
hats off to you sir, excellent explanation, and thnks a lot
@rinkishaw1929
@rinkishaw1929 2 ай бұрын
Great video ❤
@kuoyulu6714
@kuoyulu6714 Жыл бұрын
just starting my leetCode journey, with no math background, this video helped me so much, thanks a lot!
@nikoo28
@nikoo28 11 ай бұрын
I wish you all the very best :)
@kuoyulu6714
@kuoyulu6714 11 ай бұрын
@@nikoo28 thanks!
@codeflix927
@codeflix927 Жыл бұрын
it's Amazing explanation . I loved it.❤❤❤❤❤❤❤❤❤❤❤❤
@AkashYadav-di6kd
@AkashYadav-di6kd 6 ай бұрын
Thank you very much, sir.
@RakshithVrishab-ht8vk
@RakshithVrishab-ht8vk 8 ай бұрын
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
@nikoo28
@nikoo28 7 ай бұрын
really glad you feel that way 😄
@hommy4850
@hommy4850 Жыл бұрын
well explained thanks you!!
@Deepz007
@Deepz007 2 ай бұрын
Yours was the best explanation.
@sammedpatil3907
@sammedpatil3907 2 жыл бұрын
very nice method of explanation....
@ritendahiya8828
@ritendahiya8828 5 ай бұрын
Thank you sir!!
@kunalkheeva
@kunalkheeva Жыл бұрын
great content!
@apuravsharma9995
@apuravsharma9995 7 ай бұрын
Great explanation
@priyagupta5139
@priyagupta5139 Ай бұрын
Your Explanation is very good.
@nikoo28
@nikoo28 Ай бұрын
Glad you think so!
@Mr.Zeus11
@Mr.Zeus11 2 ай бұрын
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!! ❣
@nikoo28
@nikoo28 2 ай бұрын
Love it!!! 😁
@MrMichalXXL
@MrMichalXXL 7 ай бұрын
I was skeptical but in the end you delivered the understanding
@nikoo28
@nikoo28 6 ай бұрын
glad i could help you
@sreeharsharaveendra289
@sreeharsharaveendra289 6 ай бұрын
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.
@nikoo28
@nikoo28 6 ай бұрын
Excellent
@TaufeeqAhmed-hk4rz
@TaufeeqAhmed-hk4rz 4 ай бұрын
Awesome Explanation bro
@nikoo28
@nikoo28 4 ай бұрын
Thank you so much 🙂
@1tav0
@1tav0 2 жыл бұрын
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 !
@nikoo28
@nikoo28 2 жыл бұрын
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.
@santoshibora1892
@santoshibora1892 11 ай бұрын
@@nikoo28 why array is of n+1 size, why cant n??
@krishkhemani96
@krishkhemani96 5 ай бұрын
​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
@kanimozhikani7138
@kanimozhikani7138 8 ай бұрын
Thank you so much sir🎉
@data10xwithsandeep
@data10xwithsandeep 2 ай бұрын
Boss you are a king of coding and the best teacher
@nikoo28
@nikoo28 2 ай бұрын
Thank you so much
@user-qy2fm3pu2b
@user-qy2fm3pu2b 51 минут бұрын
great man...!!!!
@mohitarya5189
@mohitarya5189 Жыл бұрын
Thank you 😊😊
@latest_news_stories
@latest_news_stories Жыл бұрын
Lit 🔥very well explained
@nikoo28
@nikoo28 Жыл бұрын
You too are awesome 😎
@omarkhan5223
@omarkhan5223 11 ай бұрын
Great video, what tool are you using with your pen to make those bright red lines?
@nikoo28
@nikoo28 10 ай бұрын
that is an application called GoodNotes 6
@carlosraul6578
@carlosraul6578 10 ай бұрын
Thanks my bro
@nikhilmishra8266
@nikhilmishra8266 5 ай бұрын
observing the output we can also use simple fib program
@adnaan28
@adnaan28 2 ай бұрын
just an amazing explanation
@nikoo28
@nikoo28 26 күн бұрын
Glad you think so!
@Arcadencebeats
@Arcadencebeats Жыл бұрын
Thank you so much!
@nikoo28
@nikoo28 Жыл бұрын
You're welcome!
@SHAIKAFTABAHMED-gz9wu
@SHAIKAFTABAHMED-gz9wu 5 күн бұрын
Bro u r really a bro ....
@abhishekkeshri3974
@abhishekkeshri3974 Жыл бұрын
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.
@nikoo28
@nikoo28 Жыл бұрын
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.
@abhishekkeshri3974
@abhishekkeshri3974 Жыл бұрын
@@nikoo28 glad to know. We like to talk with you.
@rishabhsinghal5823
@rishabhsinghal5823 Жыл бұрын
very nice explanantion baat to hai..
@TrueRDX
@TrueRDX Жыл бұрын
thanks very much for explain dp 🤗
@nikoo28
@nikoo28 Жыл бұрын
Thank you so much 😊
@sathiroy4788
@sathiroy4788 Жыл бұрын
Nice explanation.
@nikoo28
@nikoo28 Жыл бұрын
Keep watching
@filmbuzz9419
@filmbuzz9419 9 ай бұрын
Nice
@rajnishroy7429
@rajnishroy7429 Жыл бұрын
thank u..
@jritzeku
@jritzeku 6 ай бұрын
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.
@nikoo28
@nikoo28 6 ай бұрын
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.
@abhishekkr8077
@abhishekkr8077 11 ай бұрын
Bahut badhiya padhate ho sir aap 🙏
@nikoo28
@nikoo28 10 ай бұрын
thank you so so much
@DeveloperJay
@DeveloperJay 4 ай бұрын
the way of explaining was very very good. but we can use simple Fibonacci approach as well.
@nikoo28
@nikoo28 3 ай бұрын
but how would you know that it is a fibonacci sequence??
@omkarkhandalkar8869
@omkarkhandalkar8869 2 жыл бұрын
Really helpfull sir The way you're explaining is >>>>>>>>>>>>>>>>>>>>>>>>> any other course or youtube channel
@nikoo28
@nikoo28 2 жыл бұрын
It will be so helpful if you tell about my channel to your friends/colleagues as well 😃
@omkarkhandalkar8869
@omkarkhandalkar8869 2 жыл бұрын
@@nikoo28 yes sir shared with community groups
@agentphoenixtm642
@agentphoenixtm642 10 ай бұрын
Isnt this is tabulation rather than memoization? As we following bottom up approach from base case to final result 13:37
@nikoo28
@nikoo28 9 ай бұрын
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.
@ujjawaltyagi3095
@ujjawaltyagi3095 4 ай бұрын
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 ?
@nikoo28
@nikoo28 3 ай бұрын
that is just to avoid index out of bounds exception
@prashanthshetty8337
@prashanthshetty8337 4 ай бұрын
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 } ```
@skrishnan9625
@skrishnan9625 5 ай бұрын
This problem can also be done with the help of recursion
@Muhammed___koya
@Muhammed___koya 13 күн бұрын
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;
@shivashankar6043
@shivashankar6043 7 ай бұрын
So, its a modified question of finonacci series problem
@nikoo28
@nikoo28 7 ай бұрын
not exactly...it just happens to have a fibonacci pattern.
@CostaKazistov
@CostaKazistov 2 жыл бұрын
Confused. Isn't this LeetCode 70 (Climbing Stairs) and not 47 (Permutations II)?
@nikoo2805
@nikoo2805 2 жыл бұрын
Thanks..this has been fixed :)
@astra8072
@astra8072 10 ай бұрын
so solution of this problem is basically a fibbonacci?
@nikoo28
@nikoo28 9 ай бұрын
you are absolutely correct. Watch another fun video on fibonacci series: kzbin.info/www/bejne/jaawhXaXgpejoZo
@sudippaudel1364
@sudippaudel1364 5 ай бұрын
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?
@nikoo28
@nikoo28 4 ай бұрын
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
@sudippaudel1364
@sudippaudel1364 4 ай бұрын
Thank you @@nikoo28
@riyamalhotra6726
@riyamalhotra6726 5 ай бұрын
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
@brunosdm
@brunosdm 5 ай бұрын
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.
@riyamalhotra6726
@riyamalhotra6726 5 ай бұрын
@@brunosdm Oh! I looked up memoization and watched the explanation by hackerrank. Thanks!
@nikoo28
@nikoo28 4 ай бұрын
absolutely correct, with memoization you can store your results..so you are not computing again and again.
@machans-203
@machans-203 2 ай бұрын
You can reduce space complexity to O(1). It's a fibonnaci series in other name👍
@nikoo28
@nikoo28 2 ай бұрын
But you need to understand the problem before you can derive the fibonacci series
@sukanyaghosh24
@sukanyaghosh24 9 ай бұрын
Sir please make a series of all the 150 neet code questions using java, it's a humble request 🥺
@nikoo28
@nikoo28 8 ай бұрын
Will upload soon
@sukanyaghosh24
@sukanyaghosh24 8 ай бұрын
@@nikoo28 thank you ☺️
@filmbuzz9419
@filmbuzz9419 8 ай бұрын
Min Cost Climbing Stairs also make vedio on these quation
@nikoo28
@nikoo28 8 ай бұрын
let me find it
@sanketsuryawanshi
@sanketsuryawanshi 2 жыл бұрын
Sir please make whole series on Tree,Graph, DP plz sir
@nikoo2805
@nikoo2805 2 жыл бұрын
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 :)
@sanketsuryawanshi
@sanketsuryawanshi 2 жыл бұрын
@@nikoo2805 Thanks sir no problem
@nikoo28
@nikoo28 4 ай бұрын
The complete playlist on graphs is now available: kzbin.info/aero/PLFdAYMIVJQHNFJQt2eWA9Sx3R5eF32WPn
@Kemba_oak
@Kemba_oak Ай бұрын
this feels exactly like Fibonacci sequence
@nikoo28
@nikoo28 Ай бұрын
Yes, but you need to arrive at that conclusion first
@sarthakjoshi21
@sarthakjoshi21 Жыл бұрын
op
@anishbhandari4699
@anishbhandari4699 Ай бұрын
why array size is n+1
@nikoo28
@nikoo28 29 күн бұрын
you need to start from 0
@anishbhandari4699
@anishbhandari4699 25 күн бұрын
@@nikoo28 but you are not using 0th index right
@gnanaprakashm843
@gnanaprakashm843 5 ай бұрын
11:44 n minus 1 and n minus 2 😅😅
@amitdahal9053
@amitdahal9053 Жыл бұрын
Thank you bhai...apke tasveer print karke main kal se pooja karunga
@nikoo28
@nikoo28 Жыл бұрын
thanks for the lovely feedback 🙏
@rickdutta3935
@rickdutta3935 4 күн бұрын
This is just a fibonacci series answer.
@nikoo28
@nikoo28 3 күн бұрын
but you need to reach over there first :)
@rickdutta3935
@rickdutta3935 3 күн бұрын
@@nikoo28 Love your videos sir❤
@melk48111
@melk48111 7 ай бұрын
you should make a udemy course and make $$
@nikoo28
@nikoo28 6 ай бұрын
Haha..what course do you think I should make?
@melk48111
@melk48111 6 ай бұрын
@@nikoo28 Udemy course on solving leetcode and another course on DSA
@TECHNICALMANedit
@TECHNICALMANedit 9 ай бұрын
this method is right or wrong @NikhilLohia when i submit the code then I give's exceded time error if(n == 1 || n == 2){ return n; } int recCall1 = climbStairs(n-1); int recCall2 = climbStairs(n-2); int smallCal = recCall1+recCall2; return smallCal;
@nikoo28
@nikoo28 8 ай бұрын
try debugging with edge cases
@TrueRDX
@TrueRDX Жыл бұрын
sub done 🤙
THEY made a RAINBOW M&M 🤩😳 LeoNata family #shorts
00:49
LeoNata Family
Рет қаралды 14 МЛН
Mastering the Final Keyword in Java: Everything You Need to Know!
10:36
Climbing Stairs
5:35
Kevin Naughton Jr.
Рет қаралды 99 М.
Climbing Stairs Leetcode
7:41
Code with Alisha
Рет қаралды 7 М.
Climbing Stairs - Dynamic Programming - Leetcode 70 - Python
18:08
I gave 127 interviews. Top 5 Algorithms they asked me.
8:36
Sahil & Sarra
Рет қаралды 611 М.
Amazon Coding Interview Question - Recursive Staircase Problem
20:01
THEY made a RAINBOW M&M 🤩😳 LeoNata family #shorts
00:49
LeoNata Family
Рет қаралды 14 МЛН