Dynamic Programming Explained (Practical Examples)

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ส.ค. 2024
  • Have you ever wondered what Dynamic Programming is? Well in this video I am going to go into the definition and the theory of Dynamic Programming! I am also going to talk to you about how to classify certain problems to know if you can use Dynamic Programming for them, and then I will show you some examples and how to optimize the solutions for them! Thanks for watching and I hope you enjoy!
    💻 ProgrammingExpert is the best platform to learn how to code and become a software engineer as fast as possible! Check it out here: programmingexpert.io/tim and use code "tim" for a discount!
    📄 Resources 📄
    BigO Notation: • Big O Notation & Time ...
    ⭐️ Timestamps ⭐️
    00:00 | Overview
    01:00 | Dynamic Programming Definition
    02:37 | Fibonacci Sequence - Problem
    05:03 | Fibonacci Sequence - Trivial Solution
    08:02 | Fibonacci Sequence - Optimal Solution
    14:39 | Minimum Sum Subarray - Problem
    15:57 | Minimum Sum Subarray - Trivial Solution
    17:56 | Minimum Sum Subarray - Optimal Solutions
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    👕 Merchandise: teespring.com/stores/tech-wit...
    📸 Instagram: / tech_with_tim
    📱 Twitter: / techwithtimm
    ⭐ Discord: / discord
    📝 LinkedIn: / tim-ruscica-82631b179
    🌎 Website: techwithtim.net
    📂 GitHub: github.com/techwithtim
    🔊 Podcast: anchor.fm/tech-with-tim
    🎬 My TH-cam Gear: www.techwithtim.net/gear/
    💵 One-Time Donations: www.paypal.com/donate?hosted_...
    💰 Patreon: / techwithtim
    ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️
    ⭐️ Tags ⭐️
    -Tech With Tim
    -Dynamic Programming
    -What is Dynamic Programming
    -Dynamic Programming Explained
    -Examples of Dynamic Programming
    ⭐️ Hashtags ⭐️
    #TechWithTim #DynamicProgramming

ความคิดเห็น • 92

  • @PrabhuKiranKonda
    @PrabhuKiranKonda 2 ปีที่แล้ว +74

    Tim, why don't you make a course on Data Structures and Algorithms? Like a complete Bootcamp. It would be really helpful to many people out there..

    • @audic2350
      @audic2350 2 ปีที่แล้ว +1

      I agree, we won't say no to data structures and algorithms videos :)

  • @jhonsuper7065
    @jhonsuper7065 2 ปีที่แล้ว +35

    Tim, I really appreciate your High Quality video, and honestly I am so happy to see that your almost at 1 million subs. You really inspire me to code, and you are a major motivation to me. Thanks so much.

    • @Vancha112
      @Vancha112 2 ปีที่แล้ว +1

      @♜ Pinned by Tech With Tim 100% genuine T.I.M.

  • @MishaSv
    @MishaSv 2 ปีที่แล้ว +23

    This is one of the clearest explanations of dynamic programming! It really helps to understand what is happening in those complicated coding questions.
    I've been watching TWT since the time the channel had 200k subs, and it is what inspired me to start my own channel with Python programming tutorials!
    1M subscribers is coming soon Tim!

    • @ppbroAI
      @ppbroAI 2 ปีที่แล้ว

      Bro, awesome explanation and the clarification that is not possible to use this with every problem is the thing most begginers need. First you need to have a specific mindset to try this.

  • @Phiz787
    @Phiz787 2 ปีที่แล้ว +1

    Excellent lesson! I was definitely able to shift my thinking during your examples, and as you were performing the human-iterating solution to the minimum sum problem I was designing the algorithm in my head. In the end, the code writes itself since the method is so clear. Thanks!

  • @benx5781
    @benx5781 2 ปีที่แล้ว

    this is so awesome you simplified the dynamic programming solution much easier i love it!

  • @gidartsproduction5439
    @gidartsproduction5439 2 ปีที่แล้ว

    I never knew how to solve programming problems and thanks to Tim, his explanation is clear and I now try at least some. For this kinda video, I appreciate a lot sir... thanks for this high quality information.

  • @kevinmutinda8429
    @kevinmutinda8429 ปีที่แล้ว +1

    For the Minimum Sum Subarray - Optimal Solutions; change the value of `min_sum_using_last_element
    min_sum_using_last_element` to `float("inf").

  • @kokop1107
    @kokop1107 10 หลายเดือนก่อน +1

    Finally a tutorial that is making sense! Most explainations are spoon bad, but of course this great quality is absolutely to be expected from one of my favourite tech channels!

  • @Vikasptl07
    @Vikasptl07 2 ปีที่แล้ว

    Thank you for simple and clear explanation!!!

  • @liamwelsh5565
    @liamwelsh5565 2 ปีที่แล้ว +3

    Off topic but I find it interesting that you write your numbers from bottom to top.

  • @chengao6404
    @chengao6404 ปีที่แล้ว

    this video is extremely helpful and inspired by dynamic programming, I appreciate the effort you made!

  • @not_vinkami
    @not_vinkami 2 ปีที่แล้ว +1

    The fibonacci function memoization is quite neat. However as a math programmer I've to say that this solution is suboptimal. The true optimal solution is in fact even faster
    def fib(n):
    gr = (1 + math.sqrt(5)) / 2
    return (gr ** n - (1 - gr) ** n) / (2 * gr - 1)

  • @universecode1101
    @universecode1101 2 ปีที่แล้ว +1

    I appreciate man 👍🏻 practical examples are ✅

  • @michelchaghoury870
    @michelchaghoury870 2 ปีที่แล้ว

    @Tech With Tim, great video learned a lot. please can you make of these vids they are really helpful

  • @melihozkocacik
    @melihozkocacik 2 ปีที่แล้ว +5

    @8:02 You can use the cache decorator from functools instead of using your own cache.

    • @ag88301
      @ag88301 ปีที่แล้ว +1

      I was looking for this comment...

  • @eduardoignacioroblessosa6349
    @eduardoignacioroblessosa6349 ปีที่แล้ว

    Crystal clear explanation , thank you!

  • @AndreasToth
    @AndreasToth 6 หลายเดือนก่อน

    After having watched this I learned that I already use dynamic programming without knowing that this is a recognised methodology with a name 😊

  • @StefanReich
    @StefanReich 2 ปีที่แล้ว +2

    16:06 The complexity is O(n^3), not O(n^2) (you forgot to factor in the call to the sum function). Easy to optimize to O(n^2) of course, but I thought I'd point it out for correctness sake and to look really smart (heh)

  • @alimihakeem841
    @alimihakeem841 16 วันที่ผ่านมา

    Thanks Tim. I love the detailed explanation. Could you help make video on data structure and Algorithm

  • @diconicabastion5790
    @diconicabastion5790 2 ปีที่แล้ว +3

    Your second Fib is overly complex and stores more than you need. 3 variables and a for loop is all that is needed.
    You don't need to store all the solution. You make the same mistake on the one after it as well. You only need to store the last solution, the min_sum, That two values are all you need to compare against the array.

    • @martinemanuel8239
      @martinemanuel8239 2 ปีที่แล้ว

      Interesting observation, but I think Tim uses this examples for DP proposes, not only to optimize space or things like that

  • @mehdismaeili3743
    @mehdismaeili3743 2 ปีที่แล้ว

    Hi,you are very good teacher.thank you.

  • @timmyx97
    @timmyx97 2 ปีที่แล้ว +1

    i = 0
    stored1 = 0
    stored2 = 1
    n = 20
    while i < n - 1:
    stored1 , stored2 = stored2 , stored1 + stored2
    i += 1
    is this O(n) as well ?

  • @ripfinity1009
    @ripfinity1009 2 ปีที่แล้ว +3

    Great video Tim.
    A quick question. The final code has some redundant bits, right? As I see it min_sum_using_last_element does not need to exist, instead you can just initialize current_min as array[0], use that in the new current_min calculation and delete the redundant line where you set min_sum_using_last_element = current_min.
    I know this is an example code but I really got fixed on this and wanted to point it out.

  • @No-uu7wm
    @No-uu7wm 2 ปีที่แล้ว

    The Fibonacci function can be further optimised with DP if you use an array instead of a dict, generating successive values by adding the last 2 elements, just need to store an extra pointer to the tail

  • @macklemo5968
    @macklemo5968 2 ปีที่แล้ว

    You're a hell of a teacher fr :)

  • @virtualizer7451
    @virtualizer7451 2 ปีที่แล้ว +11

    The time complexity at 16:07 should be O(N^3). You forgot to include O(N) coming from sum of the list

    • @gtALIEN
      @gtALIEN 2 ปีที่แล้ว +1

      @♜ Pinned by Tech With Tim bruh

  • @agustinzavala7755
    @agustinzavala7755 2 ปีที่แล้ว

    Great video, thanks 👍

  • @gokulmahajan1713
    @gokulmahajan1713 7 หลายเดือนก่อน

    Perfect, as a beginner it's difficult to get a feel of what exactly DP is, you have explained it perfectly into your min subarray sum example.
    Other channels directly jump on usual solutions like writing recursive solutions then adding memoization etc, but failed to
    explain how O(n2) converted into O(n) and how DP is actually involved in it.

  • @Daedroh
    @Daedroh ปีที่แล้ว

    @ 24:25 it is super clear. Thank you

  • @qaiserali6773
    @qaiserali6773 2 ปีที่แล้ว

    Solve more problems on dynamic programming by continuing this series

  • @Rehanshaikh-uy4vy
    @Rehanshaikh-uy4vy 2 ปีที่แล้ว

    Your videos Inspire

  • @nefwaenre
    @nefwaenre 2 ปีที่แล้ว

    Yes! So i still remember my school time's fibonacci programming and using dp (in BlueJ) to solve this! That was 14 years ago!! :D If only we had caching back then.

  • @codeyourmood8553
    @codeyourmood8553 ปีที่แล้ว +1

    if anyone don't know the minimum sub array algorithm that is used in this video is kadane's algorithm

  • @francisTriesEverything
    @francisTriesEverything ปีที่แล้ว

    def minSum(numbers: list) -> (int):
    minsum, minsum_using_elt = float('inf'), float('inf)
    for i in range(len(numbers)):
    minsum_using_elt = min(numbers[i], minsum_using_elt + numbers[i])
    minsum = min(minsum_using_elt, minsum)
    return minsum

  • @AndreasToth
    @AndreasToth 6 หลายเดือนก่อน

    I would have tested if n is NOT in the cache and, if not, then calculate and add the key-pair to the dictionary, i.e., cache, and then followed this with a common line that returns the value of the key at cache[n] (i.e., executed in either case n was or wasn't originally found). Coding it this way means no code is repeated. A way to then optimise this is to eliminate the common lookup that isn't necessary following the calculation of the value at n by assigning the value to a variable before storing it in the cache, and then return that value directly.

  • @supunsankalpa5084
    @supunsankalpa5084 2 ปีที่แล้ว

    super video. thnks

  • @shaharsarlk
    @shaharsarlk 2 ปีที่แล้ว

    Hi, great video.
    In 15:38 isn't there is another solution as the whole array?

  • @dinohunter7176
    @dinohunter7176 2 ปีที่แล้ว

    Hi Tim, I like how you explain solving methods. If possible can you provide pseudocode instead of js code? Some are not familiar with all js syntax shown, but will eventually understand the pseudocode. Thanks.

    • @rudyNok
      @rudyNok 2 ปีที่แล้ว +1

      It's python, not js.
      And you can't ask Tim to meet your beginner requirements when he's explaining more advanced programming topics.
      Just go and learn the basics, like everyone else did, is really simple :)

  • @pravachanpatra4012
    @pravachanpatra4012 2 ปีที่แล้ว +3

    Tim can you make DJANGO projects?

  • @Lunolux
    @Lunolux 2 ปีที่แล้ว

    great video

  • @neumanngregor
    @neumanngregor 9 หลายเดือนก่อน

    13:49 There is s small mistake, cache[0] = 0 and cache[1]=1, not 1 and 1.

  • @KDSBestGameDev
    @KDSBestGameDev 2 ปีที่แล้ว

    When you explain the 22:24 you explain that you include the previous element, because -5 is less then six. That's actually not the reason you include it. If the Value is -3 and the previous value is -1, it is not less, but still would lower the value of the result. You eplain it in more detail and better for other parts of the solution.
    Great video all in all thank you :).

  • @cakep4271
    @cakep4271 2 ปีที่แล้ว

    Sweet vid dude! You got them teaching skills much much better than most 👍👍👍👍

  • @danholm99
    @danholm99 9 หลายเดือนก่อน

    nice explanation. Thank you. I have one little thing....You are using af function called min twice...does that not effect O(n) ? I mean it must sort the array twice.?

  • @ikhairi83
    @ikhairi83 2 ปีที่แล้ว +1

    Tim, that's really good explanation.

  • @meg33333
    @meg33333 2 ปีที่แล้ว

    Good explanation
    Can you also make ML website or app project in 2 or 3 day like brain MRI image detection for Alzheimer disease .

  • @11yoyomama
    @11yoyomama 2 ปีที่แล้ว

    Cool video and well explained. This is a nitpick and I’m sure you know this and just didn’t want to distract from the point of the video but you shouldn’t pass empty dictionary (or list) as kwargs in Python

    • @tubero911
      @tubero911 2 ปีที่แล้ว

      I think you mean do not use mutable default arguments.

    • @11yoyomama
      @11yoyomama 2 ปีที่แล้ว

      @@tubero911 yep that’s… what I said?

    • @kailashks901
      @kailashks901 2 ปีที่แล้ว

      What is the alternative then?

    • @11yoyomama
      @11yoyomama 2 ปีที่แล้ว +1

      @@kailashks901 typically you would use *None* as your default argument then check inside the function with something like “if x is None: x={}”

    • @kailashks901
      @kailashks901 2 ปีที่แล้ว

      @@11yoyomama Oh I see. Thank you. Will keep this in mind.

  • @karma_senpai
    @karma_senpai 2 ปีที่แล้ว

    sometimes everybody get bored when doing these favourite thing.
    I have a question for you tim or anyone else do you get bored when learning programming in your life.

  • @AndreasToth
    @AndreasToth 6 หลายเดือนก่อน

    In your dynamic minimum sum example, you create a variable that's meant to store the minimum sum then forget to update it in your walk-through, well, you did so at least when you got to index 3.
    P.S. Ah, you end up correcting yourself! 👍

  • @MoughithROUIS
    @MoughithROUIS 2 ปีที่แล้ว

    Can you make a digital and an analog clock using pygame (tutorial)

  • @Rei-m3g
    @Rei-m3g 2 ปีที่แล้ว

    Optimization is simply finding the shortest path? tats y am asking the question here.

  • @cozyrain410
    @cozyrain410 2 ปีที่แล้ว

    memoization and tabulation??

  • @Kennethlumor
    @Kennethlumor 2 ปีที่แล้ว

    Sir please and please help me. I developed E-learning management systems where I want the admin sqlite3 database (user_loader(UserMixin) should be different from the students own how do I go about it. Sir please help 😢 because the project is my final year project that I will be submitting ending of this month

  • @Bunnokazooie
    @Bunnokazooie 2 ปีที่แล้ว +1

    your cache is secretly a global variable due to mutation and default arguments in python. So it works, but only because youre using some weird corners ot the the language. By accident.

    • @bernhardschmidt9844
      @bernhardschmidt9844 2 ปีที่แล้ว

      In case anyone is interested, the more elegant/pythonic way of doing the memorization would be to just add the @cache decorator from functools to the naive solution

  • @lenickramone
    @lenickramone 2 ปีที่แล้ว

    awesome

  • @nikolaoslibero
    @nikolaoslibero ปีที่แล้ว

    Why do so many people trying to explain dynamic programming use the Fibonacci sequence? It is not an example of an algorithm that benefits from dynamic programming.
    f(n-2) = f((n-1)-1), We would never need to do it twice as we could could just solve for n, so solving twice was incorrect even before applying dynamic programming.
    The latter minimum sum subarray problem was great!

  • @xskiinzy7542
    @xskiinzy7542 2 ปีที่แล้ว

    Hi im a new subscriber ive seen one of your videos of solving this code issue and was wondering if u can assist me with it which would be appreciated p.s im a noob at coding

  • @babitabarman2582
    @babitabarman2582 2 ปีที่แล้ว +2

    Hi I am from India 13 years I am able to make colon game in pygame what next language I should learn plese tell

    • @enos5192
      @enos5192 2 ปีที่แล้ว +2

      It's not about languages, it's about design patterns.

    • @convolutionalnn2582
      @convolutionalnn2582 2 ปีที่แล้ว +3

      Master python....Choose the field in which you wanna specialized...For example ,If Machine Learning ,then python is good no new language is need...If it is Android development, then dart ,Kotlin and Java are best

    • @babitabarman2582
      @babitabarman2582 2 ปีที่แล้ว +1

      @@convolutionalnn2582 best is master python 🤗

    • @convolutionalnn2582
      @convolutionalnn2582 2 ปีที่แล้ว

      @@babitabarman2582 You could already made a game...You could study more than programming language....I prefer you to choose specific field...You will Master language by implementing the field you choose

    • @amirmustafakhan7773
      @amirmustafakhan7773 2 ปีที่แล้ว

      @@convolutionalnn2582 Artificial Intelligence or Data Science

  • @RahulChauhanart
    @RahulChauhanart 2 ปีที่แล้ว

    Here is my solution for min sum subarray without DP in O(n)
    def min_sum_subarr(arr):
    if not arr:
    return 0
    minimum_sum = float('inf')
    current_sum = 0
    for elem in arr:
    current_sum += elem
    if current_sum < minimum_sum:
    minimum_sum = current_sum
    if minimum_sum > elem:
    minimum_sum = elem
    current_sum = elem
    return minimum_sum

    • @bernhardschmidt9844
      @bernhardschmidt9844 2 ปีที่แล้ว

      That's effectively the DP solution, though. The only real difference between your code and the one from the video is that you don't needlessly keep track of the best solution including each previous element (min_sum_using_element) and instead only that of the last one (current_sum). You do the comparisons a bit differently as well, but they end up doing the same thing as the min calls in the video

  • @AndreasToth
    @AndreasToth 6 หลายเดือนก่อน

    You confuse things when you say the previous element yet mean the previous minimum sum of the previous element. Things don't get better when, in some cases, you then highlight the previous element instead of the previous minimum sum of that index position.

  • @pravachanpatra4012
    @pravachanpatra4012 2 ปีที่แล้ว

    10:54

  • @AndreasToth
    @AndreasToth 6 หลายเดือนก่อน

    I really wish that you had reused the same array for the non-dynamic and dynamic version of the minimum sum problem especially for people who attempted to solve the problem using the initial array. Sorry, but your choice of changing the array for the same problem but solved differently was counterproductive.

  • @aniket3935
    @aniket3935 2 ปีที่แล้ว

    I need a laptop to start coding stuff. So...... If you have one please give me

  • @YasinNabi
    @YasinNabi 2 ปีที่แล้ว

    If you are not making money while sleeping, then you are working for others till end of your life ! Warren Buffett !
    an awesome channel and Subbed ! a fellow creator ^^

  • @logananderon9693
    @logananderon9693 2 ปีที่แล้ว

    Brain hurty

  • @parultandon8566
    @parultandon8566 2 ปีที่แล้ว

    🇮🇳

    • @parultandon8566
      @parultandon8566 2 ปีที่แล้ว

      @♜ Pinned by Tech With Tim Done. My phone's notification panel is waiting for the next Tech with Tim video.

  • @joepike1972
    @joepike1972 2 ปีที่แล้ว

    Here is my implementation of these in Forth using SwiftForth and Win32Forth.
    : FIBSTEP ( n1 n2 -- n2 n3) DUP ROT + ; \ Adds two numbers and leaves second number and sum
    : FIB ( n1 -- n2) DUP 2 < IF . ELSE 0 1 ROT 1 DO FIBSTEP LOOP . DROP THEN ; \ If n1 < 2 return n1 else loop FIBSTEP n1 times and returns n3 from FIBSTEP
    : SMIN ( n1 n2 -- n2 n3) SWAP DUP ROT + MIN ; \ Returns min of n2 and the sum of n1 and n2
    : CMIN ( n1 n2 n3 -- n4 n5) ROT ROT SMIN DUP ROT MIN ; \ Returns SMIN of n1 n2 and the min of SMIN n3
    : TMIN ( n1 n2 ... -- n) DEPTH 1 - >R .S SMIN DUP R> 1 DO CMIN LOOP .S MIN ; \ Finds min of CMIN over indefinite array

  • @CarlosGrillet-fn1lk
    @CarlosGrillet-fn1lk ปีที่แล้ว

    using dp is way more faster, look how much is the difference on my computer
    n= 40
    102334155
    fib with dp took: 0.001s
    102334155
    fib with no dp took: 36.160s

  • @ShobiPPpro
    @ShobiPPpro 2 ปีที่แล้ว

    does anybody have a leetcode/hackerrank link to the problem?

  • @mrCetus
    @mrCetus 2 ปีที่แล้ว

    I never knew how to solve programming problems and thanks to Tim, his explanation is clear and I now try at least some. For this kinda video, I appreciate a lot sir... thanks for this high quality information.