Number of Ways to Rearrange Sticks With K Sticks Visible - Dynamic Programming - Leetcode 1866

แชร์
ฝัง
  • เผยแพร่เมื่อ 11 ก.ย. 2024

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

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

    💡 DP Playlist: th-cam.com/video/73r3KWiEvyk/w-d-xo.html

  • @HieuVoPhamMinh
    @HieuVoPhamMinh 3 ปีที่แล้ว +10

    adding modulo logic to line 13 will significantly improve the runtime and can avoid time limit exceed error, seems handling big number consumes a lot of time

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

      Yes big numbers are not handled in constant time

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

      Thanks for mentioning this!

  • @keremt8727
    @keremt8727 ปีที่แล้ว +4

    The reason this works is that that all sticks have unique sizes which makes all the subarrays (without an exception) having only 1 max.
    So, even when we remove 1 max elem from the list; the remaining subarray has exactly the same properties with the initial array (1 max with all subarrays having 1 max again etc.)
    Having only 1 max always means that there is always 1 element that can be put at the end -> (n-1, k-1)
    where as for all non-max elements -> (n-1, k) for the rest of the array
    Having only 1 max for all subarrays also mean that the problem is the same for all subarrays.
    HOWEVER, if the problem stated that the sizes could be the same. Then, the subarrays wouldn't be unique.

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

    you are a saviour dear!
    please keep uploading more such dp and graph problems.
    Thanks

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

    You always explain in the most thorough way, thank you!

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

    Checking if n==0 is redundant because it will be equal to k before it could ever go to 0

    • @mehakwalia_
      @mehakwalia_ 3 ปีที่แล้ว

      That said, very nice explanation. Had something similar in mind but could not implement it. Just wondering what if we had duplicate values in the array?

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

    good explanation!

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

    Another way of wording the dp solution is... (n-1)* dp[n-1][k] represents putting any number but the largest on the right, and dp[n-1][k-1] represents putting the largest number on the right

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

    You can change base case condition to k>n (there is no way to make k stocks visible if you don't have enough sticks)- half of dp table will be skipped. Besides, when using hashmap it is really slow -(at least in java), better solution would be to use plain array.With that changes recursive solution passes time limit without problem.

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

      however the constraint do say 1

  • @user-le6ts6ci7h
    @user-le6ts6ci7h 10 หลายเดือนก่อน

    I think the naive approach from left to right is of time complexity n^3*k , not n^2*k

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

    could you please tell me how did you find the time complexity of the brute force solution........plz....

  • @Mandeepsingh-jo5cf
    @Mandeepsingh-jo5cf 3 ปีที่แล้ว

    watched it 3 times, now i get the intuition behind this.

  • @tarunkumargola7633
    @tarunkumargola7633 3 ปีที่แล้ว

    Awesome Explanation! Thanks

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

    Wow! amazing explanation!

  • @jaimeeduardo159
    @jaimeeduardo159 3 ปีที่แล้ว

    Nice explanation!

  • @abcd-sf5ur
    @abcd-sf5ur 3 ปีที่แล้ว

    Great video pal!!

  • @jiaonao5463
    @jiaonao5463 3 ปีที่แล้ว

    Thank you for the video. It is very helpful!

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

    Best explanation!!

  • @toekneema
    @toekneema 3 ปีที่แล้ว

    How many questions can you usually solve during the contests

  • @Will-su2fg
    @Will-su2fg 3 ปีที่แล้ว +1

    So basically you didn’t explain how you came up with thinking about ranging number on last position instead of on first position. Is it an inspiration from your previous leetcode experience, or is it just pure inspiration? Is there any rational for you to range number on last position? What made you think ranging on last position could solve the problem and then give it a try? I think this thinking process is actually the key point I am interested to learn

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

      Yeah, I know what you mean. I actually only came up with the O(n*n*k) by myself, but I quickly understood the O(n*k) when i read it.
      Yes, this "reverse thinking" pattern is seen in other DP problems. A recent problem i solved, Bursting Baloons, followed the exact same pattern. So I do think experience and practice can help you recognize it. But sometimes when you run out of all options you just have to try and see if reversing the problem simplifies it.

    • @Will-su2fg
      @Will-su2fg 3 ปีที่แล้ว

      @@NeetCode may be the people actually come up with this solution in the contest know the trivial implications and the secret sauce, I don’t think they are just lucky to try reverse and found it works, they must notice some deeper level of this problem imply that reverse might work

    • @Will-su2fg
      @Will-su2fg 3 ปีที่แล้ว +1

      @@NeetCode One rational I think might be why trying reverse is when you try to apply the same dp induction logic to first position, you actually require information/states from previous “path”/ previous step about how you put the number to decide this position will be seen from left or not, so in this case, you might want to solve it from back to front, with a recursive induction

    • @tejasghone5118
      @tejasghone5118 3 ปีที่แล้ว

      @@Will-su2fg It depends on question for eg in this qs its easy to think that the tallest stick or the stick at the end will definitely be visible so its easy to fix it first and recurse on remaining

  • @galexwong3368
    @galexwong3368 3 ปีที่แล้ว

    Awesome video thanks alot!

  • @kshitijbanerjee6927
    @kshitijbanerjee6927 3 ปีที่แล้ว

    Could you elaborate on the time complexity conclusion on both approaches?

    • @Will-su2fg
      @Will-su2fg 3 ปีที่แล้ว +1

      O(n * k), the dp complexity formula is:
      how-many-state-you-have * how-much-time-you-spent-on-each-state
      This problem’s dp has n * k state, you only spent a constant O(1) time on each state, so obviously it’s O(n * k)

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

    there is an interesting closed form solution using permutations

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

      Yeah I was actually going down that road and got a solution in O(n) time; however it took me nearly 2 hours and reviewing some combinatorics

  • @EshwarNorthEast
    @EshwarNorthEast 3 ปีที่แล้ว

    Why are we multiplying by n-1?
    Like (1,2) is not same as the arrangement of (2,1) right? can anyone explain the thought process behind this?

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

      Yeah its not the same.
      Let the heights be 1, 2, 3 if you place 2 at the end then that stick will not be visible, since there will be a stick of height 3 infront of 2. So we can put any of the (n-1) sticks to the right of the tallest stick (ex. we can put either 1 or 2 to the right of 3), then the problem reduces to finding Number of Ways to Rearrange (n-1) Sticks With K Sticks Visible.

  • @lettry5297
    @lettry5297 3 ปีที่แล้ว

    Your every video is worth to watch. Thanks for video
    Can you please cover this question 862- Shortest Subarray with Sum at least k

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

      Sure, I will add that to my list and solve it soon!

    • @lettry5297
      @lettry5297 3 ปีที่แล้ว

      @@NeetCode Thanks

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

    TLE even with the DP method

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

    Amazing

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

      MOD = (10**9) + 7
      @cache
      def dp(n , k):
      if n == 0 or k == 0:
      return 0
      if n == k:
      return 1
      return (dp(n-1, k-1) + ((n-1) * dp(n-1, k))) % MOD
      return dp(n, k) % MOD
      ^ this worked 75% faster

  • @jyf3136
    @jyf3136 3 ปีที่แล้ว

    It is clear and easy to understand, thx a lot

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

    What kind of sadist asks this question in an interview???