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

Комментарии • 164

  • @pcccmn
    @pcccmn 2 года назад +39

    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 Год назад +2

      I had the same issue. He explained it so much better!

    • @edoreemmanuel4250
      @edoreemmanuel4250 8 месяцев назад +1

      I am just seeing this..I love the explanation...lols....

    • @testing-js8iu
      @testing-js8iu 5 месяцев назад +1

      yes you right

  • @horcruxone
    @horcruxone 3 месяца назад +2

    Handsdown the best explanation available on youtube for leetcode 70!

  • @dirtbag1126
    @dirtbag1126 11 месяцев назад

    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  10 месяцев назад

      glad you liked it. If you want to see more content like this, consider joining my channel: ruclips.net/channel/UCT-S2ngqEBoYCM5UKuNeELgjoin

  • @Ranjan-xc5nl
    @Ranjan-xc5nl Месяц назад

    Provides sound explanation of algorithmic approach and solution, indeed worthy channel. Thanks.

  • @jamjam3448
    @jamjam3448 3 месяца назад

    Wowww. Thanks a million bro. After watching many turotials on this, yours is the best! I've understood it! Thanks

  • @yousefnegmeldin5903
    @yousefnegmeldin5903 2 месяца назад

    Thank you so much, you helped me understand dynamic programming with such a simple explanation and example.

  • @niktri6805
    @niktri6805 Месяц назад

    Really excellent explanation, the breakdown of the problem made it much more easier to understand.

  • @purnachandrasahu622
    @purnachandrasahu622 6 месяцев назад +1

    This was really helpful!!
    Keep up the good work..

  • @kunalchauhan5294
    @kunalchauhan5294 6 месяцев назад +1

    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

  • @sa35085nhs
    @sa35085nhs 6 месяцев назад +1

    I read one of comment which was recently posted, but u replied to that comment also,great !!!

  • @zb2747
    @zb2747 Год назад +7

    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.

  • @ajaygunalan1995
    @ajaygunalan1995 Год назад +1

    Dude, you nailed it. LOVE YOU!

    • @nikoo28
      @nikoo28  Год назад +1

      Glad you enjoyed it 😄

  • @vichitrakalakaar
    @vichitrakalakaar 2 года назад +3

    I tried to solve this problem using recursion but TLE.... And you just gave me best solution .... Thank you so much guru🔥💯❤️🙏

  • @fourieruddin871
    @fourieruddin871 6 месяцев назад +2

    Your excitement for teaching has no bounds 🙏

    • @nikoo28
      @nikoo28  5 месяцев назад

      thanks for your kind words

  • @susmitobhattacharyya1668
    @susmitobhattacharyya1668 8 месяцев назад +1

    Thanks for your simple explanation.

  • @arjunb9342
    @arjunb9342 Год назад

    Thank you so much... Best and Easiest explanation ever .

  • @sukanyaghosh24
    @sukanyaghosh24 9 месяцев назад

    Omg such an amazing explanation, thank you so much sir 🙏❤️

  • @srikarjonnala6509
    @srikarjonnala6509 Месяц назад +1

    such a lovely solution and explanation

  • @savitrik3596
    @savitrik3596 18 дней назад

    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

  • @kasifmansuri7552
    @kasifmansuri7552 11 месяцев назад +1

    Great explaination!!

  • @vasavi5908
    @vasavi5908 10 месяцев назад

    Great explanation.Thank you very much

  • @rubylnic
    @rubylnic Год назад

    thank you for the great explanation!

  • @ParmodYadav
    @ParmodYadav 6 месяцев назад

    Very good way to teach brother very depth understanding of the question very good

  • @user-wg6nk7ix7x
    @user-wg6nk7ix7x 7 месяцев назад +2

    Man great breakdown of this problem, simply the best!

  • @tiyashasen5020
    @tiyashasen5020 Год назад +1

    You teach amazing. Thanks for making us understand difficult concepts in a simple way🙂

    • @nikoo28
      @nikoo28  Год назад

      Glad you feel this way :)

  • @vsh-torch
    @vsh-torch 2 года назад +1

    Good one, bro. Thank you.

  • @yashthakur5712
    @yashthakur5712 7 месяцев назад

    hats off to you sir, excellent explanation, and thnks a lot

  • @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. ruclips.net/video/VXqUQYGMnQg/видео.html

  • @nerd6134
    @nerd6134 2 года назад +14

    If this guy makes a math course it’ll be one of the best.

  • @dailyprograming9003
    @dailyprograming9003 10 месяцев назад

    your explanation skill is too good

  • @shortme9374
    @shortme9374 3 месяца назад

    Hey Nikhil, I really liked your way of explanation. keep making such videos. Subscribed.

    • @nikoo28
      @nikoo28  3 месяца назад

      Thanks for the sub!

  • @hommy4850
    @hommy4850 Год назад

    well explained thanks you!!

  • @minecraftredstoneinspiration
    @minecraftredstoneinspiration Год назад

    Really well explained.

  • @ram_mandir_india
    @ram_mandir_india 8 месяцев назад

    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  7 месяцев назад

      You're very welcome!

  • @codeflix927
    @codeflix927 Год назад

    it's Amazing explanation . I loved it.❤❤❤❤❤❤❤❤❤❤❤❤

  • @AkashYadav-di6kd
    @AkashYadav-di6kd 7 месяцев назад +1

    Thank you very much, sir.

  • @_Adil.Khan_
    @_Adil.Khan_ 2 года назад +1

    I will say just fantastic!

  • @gauravkumar-ek8mr
    @gauravkumar-ek8mr 2 года назад

    Adbhoot. Love your explanation

  • @kunalkheeva
    @kunalkheeva Год назад

    great content!

  • @kuoyulu6714
    @kuoyulu6714 Год назад

    just starting my leetCode journey, with no math background, this video helped me so much, thanks a lot!

    • @nikoo28
      @nikoo28  Год назад +1

      I wish you all the very best :)

    • @kuoyulu6714
      @kuoyulu6714 Год назад

      @@nikoo28 thanks!

  • @Rajendrachoudhary-ut8ll
    @Rajendrachoudhary-ut8ll 3 месяца назад

    Awesome explanation

  • @ayaanrashid960
    @ayaanrashid960 4 часа назад

    awesome explaination bhaiya

  • @rinkishaw1929
    @rinkishaw1929 3 месяца назад

    Great video ❤

  • @Deepz007
    @Deepz007 3 месяца назад +1

    Yours was the best explanation.

  • @latest_news_stories
    @latest_news_stories Год назад +1

    Lit 🔥very well explained

    • @nikoo28
      @nikoo28  Год назад

      You too are awesome 😎

  • @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  8 месяцев назад

      really glad you feel that way 😄

  • @sammedpatil3907
    @sammedpatil3907 2 года назад

    very nice method of explanation....

  • @ritendahiya8828
    @ritendahiya8828 6 месяцев назад +1

    Thank you sir!!

  • @NiteshMaurya1111
    @NiteshMaurya1111 9 дней назад

    wow I learn now what is dp and memorization thank you sir

  • @kanimozhikani7138
    @kanimozhikani7138 9 месяцев назад

    Thank you so much sir🎉

  • @apuravsharma9995
    @apuravsharma9995 8 месяцев назад

    Great explanation

  • @user-dx4un7gg2z
    @user-dx4un7gg2z 10 дней назад

    you simplified it to a level that my dumb brain can understand. Thank you.

  • @priyagupta5139
    @priyagupta5139 2 месяца назад

    Your Explanation is very good.

    • @nikoo28
      @nikoo28  2 месяца назад

      Glad you think so!

  • @user-qy2fm3pu2b
    @user-qy2fm3pu2b 26 дней назад

    great man...!!!!

  • @Mr.Zeus11
    @Mr.Zeus11 3 месяца назад

    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  3 месяца назад

      Love it!!! 😁

  • @mohitarya5189
    @mohitarya5189 Год назад +1

    Thank you 😊😊

  • @data10xwithsandeep
    @data10xwithsandeep 3 месяца назад

    Boss you are a king of coding and the best teacher

    • @nikoo28
      @nikoo28  3 месяца назад

      Thank you so much

  • @sreeharsharaveendra289
    @sreeharsharaveendra289 7 месяцев назад

    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

  • @1tav0
    @1tav0 2 года назад +4

    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 года назад +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 Год назад

      @@nikoo28 why array is of n+1 size, why cant n??

    • @krishkhemani96
      @krishkhemani96 6 месяцев назад

      ​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

  • @TaufeeqAhmed-hk4rz
    @TaufeeqAhmed-hk4rz 5 месяцев назад

    Awesome Explanation bro

    • @nikoo28
      @nikoo28  5 месяцев назад

      Thank you so much 🙂

  • @carlosraul6578
    @carlosraul6578 11 месяцев назад

    Thanks my bro

  • @adnaan28
    @adnaan28 3 месяца назад

    just an amazing explanation

    • @nikoo28
      @nikoo28  Месяц назад

      Glad you think so!

  • @MrMichalXXL
    @MrMichalXXL 8 месяцев назад

    I was skeptical but in the end you delivered the understanding

    • @nikoo28
      @nikoo28  7 месяцев назад

      glad i could help you

  • @Arcadencebeats
    @Arcadencebeats Год назад

    Thank you so much!

  • @rajnishroy7429
    @rajnishroy7429 Год назад

    thank u..

  • @nikhilmishra8266
    @nikhilmishra8266 6 месяцев назад

    observing the output we can also use simple fib program

  • @SHAIKAFTABAHMED-gz9wu
    @SHAIKAFTABAHMED-gz9wu Месяц назад

    Bro u r really a bro ....

  • @rishabhsinghal5823
    @rishabhsinghal5823 Год назад

    very nice explanantion baat to hai..

  • @TrueRDX
    @TrueRDX Год назад

    thanks very much for explain dp 🤗

    • @nikoo28
      @nikoo28  Год назад

      Thank you so much 😊

  • @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  Год назад +1

      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.

  • @sathiroy4788
    @sathiroy4788 Год назад

    Nice explanation.

  • @filmbuzz9419
    @filmbuzz9419 10 месяцев назад

    Nice

  • @abhishekkr8077
    @abhishekkr8077 11 месяцев назад

    Bahut badhiya padhate ho sir aap 🙏

    • @nikoo28
      @nikoo28  11 месяцев назад

      thank you so so much

  • @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
    }
    ```

  • @omarkhan5223
    @omarkhan5223 11 месяцев назад

    Great video, what tool are you using with your pen to make those bright red lines?

    • @nikoo28
      @nikoo28  11 месяцев назад

      that is an application called GoodNotes 6

  • @DeveloperJay
    @DeveloperJay 5 месяцев назад

    the way of explaining was very very good. but we can use simple Fibonacci approach as well.

    • @nikoo28
      @nikoo28  4 месяца назад

      but how would you know that it is a fibonacci sequence??

  • @skrishnan9625
    @skrishnan9625 6 месяцев назад +1

    This problem can also be done with the help of recursion

  • @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

  • @ujjawaltyagi3095
    @ujjawaltyagi3095 5 месяцев назад

    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  4 месяца назад

      that is just to avoid index out of bounds exception

  • @shivashankar6043
    @shivashankar6043 8 месяцев назад +2

    So, its a modified question of finonacci series problem

    • @nikoo28
      @nikoo28  8 месяцев назад

      not exactly...it just happens to have a fibonacci pattern.

  • @jritzeku
    @jritzeku 7 месяцев назад

    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 месяцев назад +1

      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.

  • @agentphoenixtm642
    @agentphoenixtm642 11 месяцев назад

    Isnt this is tabulation rather than memoization? As we following bottom up approach from base case to final result 13:37

    • @nikoo28
      @nikoo28  10 месяцев назад

      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.

  • @Muhammed___koya
    @Muhammed___koya Месяц назад

    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;

  • @machans-203
    @machans-203 3 месяца назад

    You can reduce space complexity to O(1). It's a fibonnaci series in other name👍

    • @nikoo28
      @nikoo28  3 месяца назад

      But you need to understand the problem before you can derive the fibonacci series

  • @sarthakjoshi21
    @sarthakjoshi21 Год назад

    op

  • @sukanyaghosh24
    @sukanyaghosh24 9 месяцев назад

    Sir please make a series of all the 150 neet code questions using java, it's a humble request 🥺

    • @nikoo28
      @nikoo28  9 месяцев назад +1

      Will upload soon

    • @sukanyaghosh24
      @sukanyaghosh24 9 месяцев назад

      @@nikoo28 thank you ☺️

  • @filmbuzz9419
    @filmbuzz9419 9 месяцев назад

    Min Cost Climbing Stairs also make vedio on these quation

    • @nikoo28
      @nikoo28  9 месяцев назад +1

      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  5 месяцев назад

      The complete playlist on graphs is now available: ruclips.net/p/PLFdAYMIVJQHNFJQt2eWA9Sx3R5eF32WPn

  • @sudippaudel1364
    @sudippaudel1364 6 месяцев назад

    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  5 месяцев назад

      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 5 месяцев назад

      Thank you @@nikoo28

  • @Kemba_oak
    @Kemba_oak 2 месяца назад

    this feels exactly like Fibonacci sequence

    • @nikoo28
      @nikoo28  2 месяца назад

      Yes, but you need to arrive at that conclusion first

  • @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 11 месяцев назад

    so solution of this problem is basically a fibbonacci?

    • @nikoo28
      @nikoo28  10 месяцев назад

      you are absolutely correct. Watch another fun video on fibonacci series: ruclips.net/video/WuMTCaM2pk8/видео.html

  • @riyamalhotra6726
    @riyamalhotra6726 6 месяцев назад

    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 6 месяцев назад

      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 6 месяцев назад

      @@brunosdm Oh! I looked up memoization and watched the explanation by hackerrank. Thanks!

    • @nikoo28
      @nikoo28  5 месяцев назад

      absolutely correct, with memoization you can store your results..so you are not computing again and again.

  • @gnanaprakashm843
    @gnanaprakashm843 5 месяцев назад

    11:44 n minus 1 and n minus 2 😅😅

  • @anishbhandari4699
    @anishbhandari4699 Месяц назад

    why array size is n+1

    • @nikoo28
      @nikoo28  Месяц назад

      you need to start from 0

    • @anishbhandari4699
      @anishbhandari4699 Месяц назад

      @@nikoo28 but you are not using 0th index right

  • @amitdahal9053
    @amitdahal9053 Год назад

    Thank you bhai...apke tasveer print karke main kal se pooja karunga

    • @nikoo28
      @nikoo28  Год назад

      thanks for the lovely feedback 🙏

  • @FARDEENAHMED-vh4th
    @FARDEENAHMED-vh4th 23 дня назад

    I still didn't understand

  • @rickdutta3935
    @rickdutta3935 Месяц назад

    This is just a fibonacci series answer.

    • @nikoo28
      @nikoo28  29 дней назад

      but you need to reach over there first :)

    • @rickdutta3935
      @rickdutta3935 29 дней назад

      @@nikoo28 Love your videos sir❤

  • @melk48111
    @melk48111 8 месяцев назад

    you should make a udemy course and make $$

    • @nikoo28
      @nikoo28  7 месяцев назад

      Haha..what course do you think I should make?

    • @melk48111
      @melk48111 7 месяцев назад

      @@nikoo28 Udemy course on solving leetcode and another course on DSA

  • @TrueRDX
    @TrueRDX Год назад

    sub done 🤙