Dynamic Programming lecture #1 - Fibonacci, iteration vs recursion

Поділитися
Вставка
  • Опубліковано 5 жов 2024

КОМЕНТАРІ • 246

  • @Errichto
    @Errichto  5 років тому +265

    Fibonacci - leetcode.com/problems/fibonacci-number/
    Staircase - leetcode.com/problems/climbing-stairs/
    Min-Path Grid - leetcode.com/problems/minimum-path-sum/
    Count-Paths Grid - leetcode.com/problems/unique-paths/

    • @ivantishchenko4686
      @ivantishchenko4686 4 роки тому +5

      Thank you for posting the links. It's a great way to understand the content!

    • @evayang1398
      @evayang1398 4 роки тому +4

      I'm trying to figure out the followup of the stair problem with limitation on max k jumps. I would think do[I][j] as ith stair with at most j steps, then dp[I][j] = dp[i-2][j-1] + dp[i-1][j-1], and dp[n][k] will be final answer. however, there are a lot of constraints, e.g.: if j > i, dp[i][j] should be same as dp[i][j-1], because it doesn't make any sense if add redundant 1 more step. and code is like:
      def stair2(n, k):
      if k == 0:
      return 0
      if k < n/2:
      return 0
      dp = [[0] * (k+1) for i in range(n+1)]
      for i in range(1, k+1):
      dp[0][i] = 1
      dp[1][i] = 1
      dp[0][0] = 1
      for i in range(2, n+1):
      for j in range(1, k+1):
      if i < j:
      dp[i][j] = dp[i][j-1]
      continue
      dp[i][j] = dp[i-1][j-1] + dp[i-2][j-1]
      return dp[n][k]

    • @ayoubed-dafali1904
      @ayoubed-dafali1904 4 роки тому +2

      You're the best

    • @abdurrahaman388
      @abdurrahaman388 4 роки тому

      Can u please clarify what is the optimised way to find Fibonacci numbers

  • @lijiayi0921
    @lijiayi0921 4 роки тому +370

    Others : make a 30 minutes video with 10 seconds of usefull stuff, and add a click bait title
    Errichto : "Consider changing the speed to 1.25". Meanwhile the whole video is priceless

  • @pranavgawade1546
    @pranavgawade1546 3 роки тому +275

    Any of the FANG companies will throw thousands of dollars to have him as an employee. Still, this guy here is making free youtube tutorials for us as well as constantly contributing throughout the community. I just want to know how? Thanks a lot!

  • @alexneagu4466
    @alexneagu4466 5 років тому +256

    Hats off for you Kamil! With every video you succed to teach new things, and to inspire future programmers. I literally can't wait for some new vids from you.

  • @praveen3123
    @praveen3123 5 років тому +198

    Loved your interview with JomaTech!! I am here after that.

    • @vladespinozacatacora9421
      @vladespinozacatacora9421 5 років тому +1

      praveen s me too

    • @someonlinevideos
      @someonlinevideos 4 роки тому +3

      I’m here similarly after one with a google engineer mock interview. The way he explains his thought process is super cool!

    • @II_xD_II
      @II_xD_II 4 роки тому +1

      :O same
      how tf is it possible

  • @vladimirmakushkin9453
    @vladimirmakushkin9453 5 років тому +15

    I really appreciate that you're describing the way to approach DP problems. Many other channels just give solutions to specific problems, which does not really help to build the intuition. Your explanation is amazing, thanks!

  • @MrLuke255
    @MrLuke255 5 років тому +76

    Honestly, you're explanation is way better than the one I was told at the university. Thanks! :)

    • @jimmorrisshen
      @jimmorrisshen 4 роки тому +1

      Very true. Will share this video with my students.

    • @iTrustInTheMusic
      @iTrustInTheMusic 3 роки тому +2

      luckily your uni actually taught you this....

  • @UnicycleSoul
    @UnicycleSoul 5 років тому +8

    Mate, the way you explain dynamic programming is so much better than anything else I've heard from my professors, etc.

  • @vedantsharma5876
    @vedantsharma5876 3 роки тому +6

    The efforts you took to add captions show your dedication to articulate your content greatly. Thank you Errichto, this is the best dp tutorial I've come across and I've watched plenty!

  • @yoyobunt
    @yoyobunt 2 роки тому +1

    watched this video 1 year ago and still remember how hard it was but you make it easier to think/approach the problem with the good analysis

  • @Elon..Musk.X
    @Elon..Musk.X 4 роки тому +3

    I did my computer science degree a decade ago. Now i could only wish and dream if i had a teacher like you. Crystal clear N to the point explanation.

  • @jdiaz8933
    @jdiaz8933 4 роки тому +3

    The value per minute of these videos is incredible. Thanks so much for putting the time to do this. God Bless You.

  • @valdius85
    @valdius85 5 років тому +6

    Great job! I am so happy to see Polish coders showing up on the world stage strong.
    After choosing the wrong degree (Civil Engineering) I am now studying programming. This channel will help me with that.
    Best wishes from Polish living in Tokyo. I really hope to visit my home country on a business trip one day. One of my friend in Gdańsk is already working for a Japanese company so it is a realistic dream ;)
    Dziękuję za ciężką pracę i twój czas ;)

    • @bhargavreddy7038
      @bhargavreddy7038 4 роки тому +1

      polish coders are regarded as one of the best in india .

    • @valdius85
      @valdius85 4 роки тому

      @@bhargavreddy7038 Thank you for sharing it.

  • @alexcipriani6003
    @alexcipriani6003 5 років тому +9

    Thanks for these videos dude, I learn more on this channel than I learn in a top tier university in CS in US.

  • @gupta_samarth
    @gupta_samarth 5 років тому +3

    Yet another amazing video.
    After watching the video, I was thinking of couple of problems ( modified versions of min path ).
    Problem 1: Given NxM grid with integer values. Let P be any path from top left corner to bottom right corner. We define F(P) as maximum value along the path P, i.e. F(P) = max{ a[i][j] | (i,j) belonging to P }. Find such path P* such that F(P) has minimum value at P = P*. You can only move down or right.
    Problem 2: Given NxM grid with integer values. Let P be any path from top left corner to bottom right corner. You can only move down or right. We define F(P) as number of times you changed type of move. More exactly, F(P) = number of points in P such that ( previous point is up and next point is right ) or ( previous point is left and next point is down ). Find such path P* such that F(P) has minimum value at P = P*.
    EDIT: For problem 2 obviously the answer is F(P) = 2 when you just go all right then all down or vice versa. Let me add, there is also K given. You have to find such path P such that F(P) is atmost K [ F(P)

    • @gupta_samarth
      @gupta_samarth 5 років тому

      @Errichto Congratulations on 10k subscribers. Really awesome milestone. Also, I think you missed my question. Can you please share your ideas?

  • @dumdumbringgumgum2940
    @dumdumbringgumgum2940 2 роки тому +1

    this is the best explanation of DP under 20 mins covering popular topics 👏👏👏👏 Thank you

  • @cactusbee8474
    @cactusbee8474 4 роки тому +2

    @Errichto 13:28 subtitle should be : And every state you compute in constant time, dp[i][k] = dp[i-1][k-1] + dp[i-2][k-1] + .. dp[i-k][k-1]

  • @SanjeevSingh-lg2sb
    @SanjeevSingh-lg2sb 5 років тому +2

    Thanks Errichto for your channel. It is helping me personally. You are doing one of the best things for all the budding college students and engineers.
    I hope you continue your effort. Love from India.

  • @BlockDesignz
    @BlockDesignz 4 роки тому +2

    No one else made me understand DP until you came along

  • @ManishSharma_msinvent
    @ManishSharma_msinvent 3 роки тому +1

    Errichto thanks for putting this up. This is as precise as it can get.

  • @therealsey
    @therealsey 3 роки тому

    The maximum-path sum problem was impossible for me to understand up until 19 minutes and 46 seconds ago. Thank you Errichto!!! If I happen to make it past my coding test on May 11th, I will owe it to this lecture series.

  • @victorariasvanegas7407
    @victorariasvanegas7407 5 років тому +7

    I come from the Joma channel, you're really incredible, I'm new in this world, and I hope to learn a lot from your channel.

  • @sauravpaul4131
    @sauravpaul4131 5 років тому +4

    Best tutorial about dp so far .Thanks.

  • @CrystalSergeant
    @CrystalSergeant 5 років тому +15

    pure gold. looking forward for next video

  • @pushkarsoni8927
    @pushkarsoni8927 5 років тому +4

    Seriously quality of information is very good

  • @ankitbansal2303
    @ankitbansal2303 4 роки тому +1

    Thank you, your videos are purely based on content quality and that's the best part.

  • @MrDivad006
    @MrDivad006 5 років тому +3

    Please make a playlist where you solve leetcode questions. Just record yourself solving the leetcode questions: make one video per question (you solving it and commenting on the problem/solution), put the video into a playlist, and repeat for as many problems as possible. This would attract so many more people to your channel

  • @chikajinanwa9179
    @chikajinanwa9179 4 роки тому +1

    Great video Kamil. I particularly enjoyed your explanation about dimensions of the state and transitions. Thank you very much!

  • @CS-lk7ym
    @CS-lk7ym 2 роки тому +3

    hey errichto! These videos are awesome and best

  • @parthmaheshwari2186
    @parthmaheshwari2186 5 років тому +5

    Great Insights on how to approach dp problems. I request for more dp content as it is hard to get comfortable with.

  • @slyace1301
    @slyace1301 3 роки тому +1

    Thank you so much for this man, I've never been able to perceive recursion

  • @ManojKumar-hj7fh
    @ManojKumar-hj7fh 4 роки тому +3

    Hello Errichto, it would be great if you could do a series on recursion and how to think of an approach to write code for recursive problems, it can be useful for solving trees, strings, graphs and dp and a lot more, do consider making vidoes on recursion
    Thank you for the knowledge

  • @amritbhardwaj99
    @amritbhardwaj99 4 роки тому +1

    Very precise and clear Explanation Kamil!!

  • @watansahu2190
    @watansahu2190 5 років тому +3

    Sir I'm really glad to have come across your videos,earlier I had came across the FFFTTTTT for binary search explanation in textual tutorials but always felt it hard to make sense, thanks for your such crystal clear detailed explanation that brought such clarity.
    I wish you could make tutorials on matrix exponentiation and how its used for DP .
    And just one wish that you also make similar video tutorials on Trees and Graphs and with links from where to practice them.I would be really grateful since Im preparing for interviews it would be of great help. Thank you sir..

  • @stilllearning5982
    @stilllearning5982 4 роки тому +1

    Thank you for the video. It helped a lot to know how to think to reach a solution for DP

  • @vamsikrishnavalluru611
    @vamsikrishnavalluru611 5 років тому +5

    Lovely methods man! Can you make a lecture series on how you approach different levels of Graph Theory problems? For examples how you solve DFS/BFS based problems, then Djikstra's Algorithm, Topological sorting, min-cut max flow ... and so on? I ask this because a lot of us know this in theory but its very difficult to implement solutions in a competitive coding contest.

  • @lovleshbhatt7797
    @lovleshbhatt7797 5 років тому +1

    This video is Gold For understanding the dp working.... LOVE YOU BRO FROM INDIA....

  • @LogicEu
    @LogicEu 2 роки тому +1

    This is an incredible learning resource, thank you!

  • @Chandansingh374
    @Chandansingh374 5 років тому +10

    Thanks for dp lec😀, please upload more such tutorial.

  • @bhargavreddy7038
    @bhargavreddy7038 4 роки тому +3

    if you explains topics like this in such a less time , i don't think i would want to look for any other instructors for all other algo ds topics . Please make videos on all algo ds topics . Your videos will become go to videos in future for everyone. ♥️

  • @samarth319
    @samarth319 5 років тому +2

    Thank you kamil! Please make a video on Graph Algorithms as well. Really appreciate it.

  • @Vivek.Rathi.53
    @Vivek.Rathi.53 5 років тому +2

    Very nice video sir. There is always something to learn from your every video.

  • @peteco7417
    @peteco7417 5 років тому

    Also brought from JomaTech Video, I'm just here because I admire your attitude and willingness to share. I don't plan on making this a career but instead use as a tool to challenge myself to churn solutions to problems. Thank You

  • @raghav4711
    @raghav4711 5 років тому +61

    @errichto: in the last minimum path sum, shouldn't we take the minimum of the previous of the two values (left and top) instead of the maximum?

    • @Errichto
      @Errichto  5 років тому +44

      Yep, you are right. I corrected it in captions but the video will stay with this mistake forever :D thanks for noticing!

    • @Errichto
      @Errichto  5 років тому +11

      @Tyler Durden How? In UA-cam editor, I only see an option to add blur.

    • @Paradise-kv7fn
      @Paradise-kv7fn 4 роки тому +13

      @@Errichto or you can pin this comment chain to the top

    • @anarbay24
      @anarbay24 3 роки тому

      @@Errichto I think it is possible, since in this video, you already corrected one mistake, where you put "*Space" word in the video

  • @sudhamani5149
    @sudhamani5149 4 роки тому +1

    Wow! This video has made dp much easier for me. I find this extremely helpful. Thanks to you!

  • @ankishnayakcs0196
    @ankishnayakcs0196 3 роки тому

    I was really struggling with dp and your videos helped me allot . Thanks

  • @that_funny_guy496
    @that_funny_guy496 5 років тому +3

    Thank you so much Errichto, your explaining style really helped me a lot.

  • @sagarchandradas6326
    @sagarchandradas6326 5 років тому +2

    We need more dp lectures which is needed to competitive programming.

  • @avinashchaubey8361
    @avinashchaubey8361 5 років тому +4

    thanks... a lot for such video lectures it really helps a lot eagerly waiting for next dp lecture with some interesting problem

  • @doodo_1
    @doodo_1 4 роки тому

    Thanks for the video, I was facing problems while learning dp ,I saw half of the video and was able to solve all the four problems.

  • @alihamdar5916
    @alihamdar5916 5 років тому +1

    finally someone made dp looks simple.. this will help me alot thx

  • @AbjSir
    @AbjSir 6 місяців тому

    Dimension of dp is the number of variables which can completely define a state

  • @swapnilmeshram9991
    @swapnilmeshram9991 5 років тому +1

    Thanks for this awesome video. I was able to solve all four problems. Previously I got the min-path problem in an interview.

  • @danielk8452
    @danielk8452 5 років тому +2

    Thank you just made the DP really easy to understand when and how to use

  • @ahmednesartahsinchoudhury2628
    @ahmednesartahsinchoudhury2628 4 роки тому +1

    Great video! Hope you make many more of these.

  • @xbz24
    @xbz24 3 роки тому +1

    high quality content as always errichto

  • @drzewo3713
    @drzewo3713 3 роки тому +5

    Dynamic programming Lecture #0 :
    Intro : 0:05
    When do we use DP ? : 0:45
    Iteration vs Recursion : 1:44
    Fibonacci Series : 3:39
    Factorial : 7:36
    Stairs : 8:33
    Minimum Path Sum : 14:04
    Summary : 18:01

  • @avishekdutta1901
    @avishekdutta1901 4 роки тому +1

    Huge RESPECT to u Sir!!!!
    Please dnt stop and keep doing the good work....

  • @surajnehra3031
    @surajnehra3031 4 роки тому

    Hi Kamil i am a great fan of yours. I watch all your videos and you are my motivation for competitive coding. I wish you all the best for this codejam 2020.

  • @ouyaah
    @ouyaah 4 роки тому +1

    Clear explanations and nice graphs, thanks!

  • @JL-pg4pj
    @JL-pg4pj 4 роки тому +1

    Please next time add some simulation of dp array by creating a box with every medium/hard problems. It will be better to understand for us in less time.

  • @Qwantopides
    @Qwantopides 4 роки тому +1

    Thank you Errichto! I have known how DP worked, I could apply it, but I didn't really understand it. By that I mean, that some dots haven't connected in my head yet. Like what you have shown here. What is the real reason behind doing it. What if you do it differently. How does the method extend in certain scenarios. You have managed to explain this very simply, for which I thank you sir. :)

  • @shrad6611
    @shrad6611 5 років тому

    first time I saw you talk so much
    great explaination sir errichto

  • @yehah
    @yehah 3 роки тому

    Great video. Didn’t need subtitles as what you said is clear.

  • @MehbubulHasanAlQuvi
    @MehbubulHasanAlQuvi 5 років тому +4

    This is what I've been looking for

  • @cakdham4607
    @cakdham4607 5 років тому

    Genius, you have best explanation with straigh to the point

  • @ariabanazadeh1016
    @ariabanazadeh1016 5 років тому +3

    Hi Thank you for your amazing lecture and please in other lectures go throught some code forces dynamic programming questions beacuse there is a lot of cllasical dp toturials but i dont think there is a lot about dp in code forces problems.

  • @chatrughanprasad7778
    @chatrughanprasad7778 4 роки тому

    Absolutely Gold...about solving dp problem

  • @SuperArjun11
    @SuperArjun11 5 років тому +3

    Fantastic as always tbh. How many problems do you think you're going to go over?

    • @Errichto
      @Errichto  5 років тому +1

      I have no idea, to be honest.

  • @abeshekr7308
    @abeshekr7308 5 років тому +1

    Great video on dp! Hoping to see many more videos about competitive programming

  • @AbjSir
    @AbjSir 6 місяців тому

    dp is used to save time which gets wasted into doing repeated work
    Generally this happens when one state depends on more than one state

  • @geoffl
    @geoffl 3 роки тому

    Now I understand!:
    - I watched this 3x
    - did the problems you linked

  • @franciscneculau3128
    @franciscneculau3128 5 років тому +2

    This content is gold ! Thank you !

  • @lukas041293
    @lukas041293 5 років тому +10

    Sorry if i missunderstood something but i have a question:
    Always you can solve a dp problem in both (iterative and recursive) or might be problems that is possible only recursively or iterative?

    • @BlockDesignz
      @BlockDesignz 4 роки тому +5

      Lucas Guerrero Any recursive problem can be solved iteratively.

  • @donbasti
    @donbasti 4 роки тому +2

    Amazing channel !. Would love to see some Discrete Math and Graph Theory :)

  • @ritamchakraborty1390
    @ritamchakraborty1390 3 роки тому

    Please create a series on graphs and graphing algorithms

  • @tritranminh2808
    @tritranminh2808 3 роки тому +1

    Really, thank you so much, it does help a lot.

  • @kamalhm-dev
    @kamalhm-dev 5 років тому +2

    I wish you provide the algorithms too

  • @jayagrawal3468
    @jayagrawal3468 3 роки тому

    really good!! Add playlist for other topics also

  • @sanjaysingh5007
    @sanjaysingh5007 5 років тому +9

    awesome video sir , learned a lot...

  • @jiachengcheung5447
    @jiachengcheung5447 3 роки тому

    You lectures are amazing

  • @firefangful
    @firefangful 4 роки тому

    Please go through atcoder knapsack problems(both variations) and make a video on them

  • @malefetsanelenka8013
    @malefetsanelenka8013 7 місяців тому

    Best explanation ever.

  • @daved1113
    @daved1113 2 роки тому

    Fantastic series. Thank you.

  • @shreyashshukla9773
    @shreyashshukla9773 4 роки тому

    Sir please make more videos on Dynamic Programming.

  • @zakwanmachhaliya6313
    @zakwanmachhaliya6313 4 роки тому +3

    I don't understand the at most k jump what is means?
    1: we can come current position from last k position
    2: we can make only k or less jumps to reach top

  • @luasantilli5814
    @luasantilli5814 4 роки тому

    My notes, feel free to iterate if I got something wrong or if there is something that should also be here.
    Usually, when a recursive/naive solution repeats some form of computation multiple times, it is smarter to save and reuse that computation
    Most of DP problems belong to one of these 3 types (counting, optimization, confirmation)
    For counting, think of combinatorics first
    For optimization and confirmation think of greedy first
    There are two approaches to dynamic programming: iteration and recursion
    Iteration is running time is faster, easier to understand the complexity, has shorter code, and more advanced techniques
    Recursion is easier to apply, could have fewer states, and the order does not matter. Just do the computation and add memoization
    Most people in competitive programming do the iterative approach because of the faster running times and availability of more advanced techniques
    [MINE] Dynamic programming appears to trade space for faster running times
    Implementation of Fibonacci
    Dynamic programming is not necessary for factorial. Since it has a linear structure.
    The staircase problem has deep structural similarities to the Fibonacci
    Minimum path sum problem explanation
    It is useful to think of all of the relevant information at each position. What do you need to remember at each position?
    Think about what element would be best to start the array with. In minimum problems, it could be infinity

    • @algorithm808
      @algorithm808 3 роки тому

      Don't forget : What is important so far ?

    • @amanrubey
      @amanrubey 2 роки тому

      ok i'm at a certain position now what do next? pls help me with this. i'm lost here

  • @aiman_yt
    @aiman_yt 6 місяців тому +1

    I'm convinced Errichto was sent by God

  • @alexwindy3
    @alexwindy3 4 роки тому

    Wow i spent weeks in high school and uni on this shit and didnt understand absolute shit. And in 20 minutes you just told me everything I didnt know good enough

  • @JohnWangCH
    @JohnWangCH 4 роки тому

    Thank you for sharing this. It is really helpful.

  • @jimmorrisshen
    @jimmorrisshen 4 роки тому

    Best DP video ever. Thanks.

  • @rickyputra98
    @rickyputra98 5 років тому

    Great video! Maybe consider add code snippet to make the video more clear

  • @sarveshdubey9347
    @sarveshdubey9347 5 років тому +3

    Amazing just amazing

  • @DHRUVNARAYANSINGH
    @DHRUVNARAYANSINGH 4 роки тому

    Thanks for sharing awesome DP tutorial.

  • @barakode414
    @barakode414 4 роки тому

    Thank you Kamil, you are the best!!

  • @youveron
    @youveron 2 роки тому

    0:5:46 imho should be N+1 to include the last index as well

  • @thuanvo9283
    @thuanvo9283 3 роки тому

    Thank you, Errichto!!!

  • @mdshakilhossen2755
    @mdshakilhossen2755 4 роки тому

    I am a big fan of you from Bangladesh.please upload some video on graph theory.

  • @GospodinStanoje
    @GospodinStanoje 4 роки тому +1

    In the last problem can someone explain to me why wouln't we just consider doing everything backwards greedy?
    If we are allowed to move only right and down, can't get move from the destination [row][column] to the start but allowed to move LEFT and UP instead using greedy algorithm. Why wouldn't that be a good way?

    • @adityagupta5713
      @adityagupta5713 4 роки тому

      Consider:
      1 1 1 1
      1 1 9 2
      1 9 1 3
      Your algorithm would move from 3 to 1 (left) but then would be forced to pick a 9, whereas a dp solution would correctly pick the 1-1-1-1-2-3 solution. You could I guess also implement a dp solution backwards, kind of like you described, but this is probably harder to implement and therefore unnecessary.

  • @karthikmucheli7930
    @karthikmucheli7930 5 років тому +3

    The minimum sum path can also be achieved by doing a BFS ?