Dynamic Programming-Paint Fence

Sdílet
Vložit
  • čas přidán 24. 07. 2024
  • Detailed Dynamic Programming Tutorial on Leetcode Problem Paint Fence.
  • Věda a technologie

Komentáře • 84

  • @jasonshen4404
    @jasonshen4404 Před 4 lety +2

    This solution is so elegant. Before this, whenever I wanna do a DP problem, I also set up an array to save the states value. Thank you Miss. Wright!

  • @bigray712
    @bigray712 Před 6 lety +13

    You should make more videos. These are timeless. thx

  • @anusatyachoudhary7382
    @anusatyachoudhary7382 Před 3 lety

    Thank you for this. Thorough explanation with visuals along with implementation. Helped me understand the concept behind the problem.

  • @iliassti4246
    @iliassti4246 Před 5 lety +1

    Great explanation. Did a good job explaining the combinatorics part, also explained how and why the code is written the way it is to avoid bugs at 13:30 Most tutorials would just write the code that implements the algorithm without explaining how. Even with the typo, we can still understand where she's going.

  • @rashmirao5312
    @rashmirao5312 Před 5 lety

    Loved the way explained! Please do make more videos!

  • @hiteshyadav2423
    @hiteshyadav2423 Před 5 lety

    That is a very nice explaination. Thank you. Hoping to see more videos like this one.

  • @yumochi8357
    @yumochi8357 Před 5 lety +2

    Thank you so such clear explanation, and your visualization were really helpful! Props!

    • @kzu8976
      @kzu8976 Před 5 lety

      Yes! Upvote for the visualization!

  • @devyashsanghai585
    @devyashsanghai585 Před 6 lety +2

    Great Explanation! Thank you. Really Helpful!

  • @abhishekanand5362
    @abhishekanand5362 Před 3 lety

    This was the best explanation, I have ever seen. Thank you.

  • @anujkhandelwal14
    @anujkhandelwal14 Před 3 lety

    This was really helpful. You made it really easy to understand! Thanks!

  • @Official-tk3nc
    @Official-tk3nc Před 4 lety +5

    Mam why did you stop uploading videos? we need this type of teachers.

  • @isarwarfp
    @isarwarfp Před 6 lety +20

    @Miss Wright you have a small error in slide at time 6:18 Where you have mentioned 2 posts answers
    2 posts answer.same + different
    k + k * (k+1)
    Shouldn't it be
    k + k * (k -1) (minus sign)

    • @misswright7247
      @misswright7247  Před 6 lety +4

      Thank you! You are correct. The k+1 should be k-1. I didn't notice it until now.

  • @ramkrishnakulkarni8289

    Nice explanation. I wish I had a teacher like you in school. Thank you.

  • @satya1067
    @satya1067 Před 4 lety +1

    Nice explanation ...mam.
    keep uploading these kind of dynamic programming problem videos.
    this will help us.

  • @potinmak1875
    @potinmak1875 Před 3 lety

    Please make more videos on Leetcode problems. I really like you explanations!

  • @manmaybarot4768
    @manmaybarot4768 Před 4 lety

    The BEST explanation. THANK YOU :)

  • @prapulkrishna
    @prapulkrishna Před 3 lety

    Fantastic Explanation. Clear and jusss perfect. You are a good teacher thanks for the video. Please upload more videos if time allows you to. Regards.

  • @hukanamatata9267
    @hukanamatata9267 Před 6 lety

    Thanks much for your explanation. It really works for me.

  • @exdunn
    @exdunn Před 2 lety

    Great explanation, the intuition for this one is pretty tricky.

  • @xinhuang7149
    @xinhuang7149 Před 4 lety

    Clear explanation! Thank you!

  • @clairechen7164
    @clairechen7164 Před 3 lety

    Good explanation!!! Very helpful!

  • @gauravkungwani
    @gauravkungwani Před 4 lety

    Thank you for such an easy solution !!!

  • @SUIJGHRHS
    @SUIJGHRHS Před 5 lety

    Thanks, so clean explanation.

  • @iphoneeating
    @iphoneeating Před 4 lety

    Thank you so much, I would really appreciate if you could make more Leetcode solution videos :)

  • @ningliu7846
    @ningliu7846 Před 2 lety

    Pretty clear and easy to understand. thx.

  • @prachurjyabasistha4682

    Wow!...such sweet explanation..please make more videos on dp problems.

  • @dipeshbudhiraja8557
    @dipeshbudhiraja8557 Před 6 lety

    Nice explanation. Thanks

  • @sayantankundu973
    @sayantankundu973 Před 2 lety

    Really really helpful... Thanks

  • @wentingsong9435
    @wentingsong9435 Před 3 lety

    Amazing video! you should make more

  • @AkashVerma-mg7ys
    @AkashVerma-mg7ys Před 4 lety

    You explained it nicely.Make more video on dp

  • @sama19778
    @sama19778 Před 7 lety

    Thank you miss wright. Good tutorial. 😃

  • @rakeshdas32
    @rakeshdas32 Před 4 lety

    Thanks A lot....simple helpful..

  • @shachisinghal8856
    @shachisinghal8856 Před 6 měsíci

    Finally understood ❤❤❤

  • @abhishekrbhat8919
    @abhishekrbhat8919 Před 5 měsíci

    great solution

  • @pasito287
    @pasito287 Před 2 lety

    really we can think like that thanks a lot mam

  • @guowanqi9004
    @guowanqi9004 Před 5 lety +1

    can you make more leetcode answer tutorials?

  • @vineetsrivastava388
    @vineetsrivastava388 Před 10 měsíci

    Brilliant 👏 👏 👏

    • @vineetsrivastava388
      @vineetsrivastava388 Před 10 měsíci

      Kindly post more videos...there are lots of dynamic programming problems...at least there should be 20 such videos

  • @ronaldabellano5643
    @ronaldabellano5643 Před 4 lety

    Miss Wright is a genius!

  • @user-wp5ut9du5r
    @user-wp5ut9du5r Před 4 lety

    不错,well done

  • @piyushnaithani7689
    @piyushnaithani7689 Před 4 lety

    Hey maam, n=4, k= 3 , it gives 66 if draw all 66 according to the algorithm, then it causes conflict that more than 2 adjacent post have same color, but I think real ans is 60

  • @jigarzanzarukiya1585
    @jigarzanzarukiya1585 Před 5 lety

    nice explanation

  • @RagazzoKZ
    @RagazzoKZ Před 4 lety

    Very useful 👍

  • @pranavtarak7512
    @pranavtarak7512 Před 7 lety +1

    Thk you

  • @ajain11337
    @ajain11337 Před 4 lety

    Thanks a lot.

  • @Alexey1st
    @Alexey1st Před 3 lety

    At 6:16 I didn't get why it would be (k-1) colors to paint 3rd post in different case. For me it looks like K. For example, we painted the first post in Blue, then for the second one we have 2 choices: red and yellow (K-1). But the third one can be any again: Red, Blue or Yellow. Which is K. How did we get K-1. Could someone explain please?

    • @misswright7247
      @misswright7247  Před 3 lety +1

      In the equation for case 2, the next post must be different, so it is k-1 to make it different than the previous. Case 1 covers when the next post is the same. So yes, there are K or 3 possibilities for the 3rd post, but because this is broken into two cases, case 1 covers 1 color, and case 2 covers the other 2 colors.

    • @Alexey1st
      @Alexey1st Před 3 lety +1

      @@misswright7247 Thank you!

  • @sumanthedara4177
    @sumanthedara4177 Před 3 lety

    woww,mamm, super

  • @dipeshkishan
    @dipeshkishan Před 4 lety +1

    Mam, please make more videos on such tricky DP Questions. why did you stop making videos ? :(

  • @saswatibhattacharjee7387

    Thank you for your explanation. I have some doubts at 2:41 where you mentioned if the two post have same color. If you paint 1st post with yellow color and as there is a catch that two adjacent posts may be in same color then second one must have k choices. So the same color number of choices should be k*k and not k*1. May be I am wrong. Can you please explain it again?

    • @misswright7247
      @misswright7247  Před 5 lety +2

      If the second post has to be the same as the first, then you had k choices for the first, but after you pick the first you only have one choice for the second which is whatever color you picked for the first. The reason you don't have k choices for the second is because that would mean the second could be any of k colors, which may or may not be the same as the first.

    • @saswatibhattacharjee7387
      @saswatibhattacharjee7387 Před 5 lety

      @@misswright7247 Thank you Mam. So what would be the generalized formula for this problem?

  • @anonsurf6640
    @anonsurf6640 Před 4 lety +1

    I remember I struggled to grasp this question for a long time. I did end up solving it but in real interviews I'd probably struggle big time. What's the secret? Practice and Pratice?

    • @anonsurf6640
      @anonsurf6640 Před 4 lety +1

      and to boot, this question is marked Easy in Leetcode...

    • @misswright7247
      @misswright7247  Před 4 lety +2

      I think with practice you can get better, simply because you have seen more problems so you can begin to see connections. Practice will help, but it's not a 100% guarantee you will not struggle and unrealistic to expect to arrive at that point. Your interviews are supposed to measure how you think, not expect you to solve a difficult problem in less than an hour. Your interviewer is supposed to help you out if you struggle, which should also be a test in how you collaborate with colleagues.

  • @irvinge4641
    @irvinge4641 Před 4 lety

    Do you know the recursive solution to this problem? Can you explain how this is a candidate to dynamic programming? The formula seems pretty tricky

    • @misswright7247
      @misswright7247  Před 4 lety

      For the recursive solution you could have another function set up similar to this, except the for loop would be the recursive call to a function that would essentially do the same thing as the for loop. If you watch my other 3 part video on the Fibonacci number, I go over how to set up a recursive solution and how to transform it into a dynamic programming solution.

    • @misswright7247
      @misswright7247  Před 4 lety +1

      Also, typically anytime you are storing old values and using those previously calculated values to calculate the final answer, then you're problem is a candidate for dynamic programming. It may be more complex than that, but that's the main thing I see.

  • @pranavtarak7512
    @pranavtarak7512 Před 7 lety +1

    How the 3 post will have k-1 choices if we use different colours,

    • @misswright7247
      @misswright7247  Před 7 lety +4

      The 3rd post needs to be different than the 2nd, so any color except 1( The 2nd Color). K - 1, means all the colors except the second, or all the colors except the previous color. At first I thought it would be K-2 for the 3rd post, but the 1st and 3rd colors do not have to be different.

    • @misswright7247
      @misswright7247  Před 5 lety

      @@buttekaustubhpradeep5566, the 2nd and 3rd post can have the same color, as long as it's not the same as the 1st post. In other words, if post 2 and 3 are the same, your only option for the first post is that it be different than post 2 and 3, otherwise you would have 3 consecutive posts the same color, which is a violation of the constraint.

    • @buttekaustubhpradeep5566
      @buttekaustubhpradeep5566 Před 5 lety

      @@misswright7247 What i meant to say is when we are counting no of ways for 3 posts we are doing same=k*(k-1)*1 ...i understood this but when calculating diff =k*(k-1)*(k-1) we are saying that first two posts are different then why is the no of options for the third post (k-1) and not k..because if we are considering that the first two colors are different then the third post can be any of the k colors, right? example the first two posts are red-yellow then the third post can be either red or yellow or green because that will not violate the constraint of more than 2 consecutive posts with same color....I am missing something in the? Thanks a lot for the quick reply :)

    • @misswright7247
      @misswright7247  Před 5 lety +1

      @@buttekaustubhpradeep5566, we are already counting the scenario when the 3rd post is the same, in the equation for same(red-yellow-yellow). Therefore the equation for different must subtract one choice for the 3rd color(red-yellow-red or red-yellow-blue), but not (red-yellow-yellow) again.

    • @buttekaustubhpradeep5566
      @buttekaustubhpradeep5566 Před 5 lety

      @@misswright7247 oh okay now i understood clearly..Thanks a lot for the quick replies madam. Hoping to see more videos on your channel :)

  • @b_1729-j8j
    @b_1729-j8j Před 4 lety

    Thanks Mam 😊😊

  • @Manu-wb2uv
    @Manu-wb2uv Před 3 lety

    Ulalala. I Get it.. This problem is quite hard :D

  • @mdlwlmdd2dwd30
    @mdlwlmdd2dwd30 Před 2 lety

    Only part I am not clear is when we have 1st 2 different, why are we multiplying k-1? shouldn't we be multiplying k-2??

    • @misswright7247
      @misswright7247  Před 2 lety

      The third does not have to be different from the first. It needs to be different from the previous, so 1 is subtracted, and that 1 is for the previous.

  • @ziliweng
    @ziliweng Před 6 lety

    Hi, Miss Wright: I think that you made a typo in listing the answer for 2 posts. it should k + k(k-1), not k + k*(k+1)

    • @misswright7247
      @misswright7247  Před 6 lety

      Wesley Z. W, you are correct. I'll try and fix it in the subtitles soon. Thank you!

    • @Pradeep.Poonia
      @Pradeep.Poonia Před 4 lety

      @@misswright7247 Its at 5:38. Just write this in pinned msg. or Pin Wesley's comment. People will understand.

  • @jain007neeraj
    @jain007neeraj Před 4 lety

    What will happen when k is less than number of post.

    • @misswright7247
      @misswright7247  Před 4 lety

      The value of k adds another layer of complexity. For example, if k is 1 and there are more than 2 posts, it would violate the constraint that no more than 2 consecutive posts can be the same color. Other than 1 or 0 for k, I can't think of how the value of k may effect the solution.

  • @RahulGupta-yp7ii
    @RahulGupta-yp7ii Před 3 lety

    mam please make more videos

  • @cstlabs1772
    @cstlabs1772 Před rokem

    T(2) = K^2 , not k + k(k+1) ; should be k + k(k-1)

  • @anokhkishore
    @anokhkishore Před 4 lety

    Almost slept ..

  • @satyarajadasara9000
    @satyarajadasara9000 Před 4 lety

    nice explanation