Maximum Value of K Coins from Piles - Leetcode 2218 - Python

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

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

  • @peterdavidmora-stevens9185
    @peterdavidmora-stevens9185 ปีที่แล้ว +24

    Even if it's not the most impressive accomplishment, I made it into Uber's Career Prep program due to your vides neetcode, and I continue to prep for my official internship interview with them through your videos too, so wish me luck with my LC journey.

    • @NeetCodeIO
      @NeetCodeIO  ปีที่แล้ว +7

      Congratulations and best of luck! Your hard work will pay off!!

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

    I love you neetcode

  •  3 หลายเดือนก่อน +1

    I always find the top-down approach much more understandable than bottom-up.

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

    btw 3d dp is giving mle , thanks for the solution though!

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

    In the decision tree, once we select 1 from first pile, and we move to second pile, and select 7 the parameters should be 1,0 isn’t it?

  • @MP-ny3ep
    @MP-ny3ep ปีที่แล้ว

    Great explanation as always

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

    Thank you for this explanation!

  • @nizarkadri3109
    @nizarkadri3109 ปีที่แล้ว +6

    I don't think anyone could come up with this solution during a 30 min interview, or its just me ?

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

      I understood what I need to do but no way on earth would I be able to code it lol

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

      @@annoyingorange90 you can do it man!only if you hve seen this problem atleast once before it appears in interview...

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

      Competitive programming peeps probably could, keeping in mind that they practiced such problems for years.

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

    Thank you so much

  • @duongphan219
    @duongphan219 4 หลายเดือนก่อน

    thank you :orz:

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

    Nice explanation! keep posting coding solutions vineet!

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

    thanks bro

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

    Thank you for the explanation, helped a lot! I was just wondering why the caching is needed as this is my first time encountering a caching problem. Could anyone explain to me? Thanks in advance!

    • @Me-kt3gh
      @Me-kt3gh 2 หลายเดือนก่อน

      I'm a year late, but for anyone else wondering:
      In this example, caching stores values when we calculate them because we may use them again soon. And in short, it is faster to read from the cache than it is to calculate the value again.
      The Depth-First Search approach explores all possibilities in all ordered, which can lead to revisiting the same subproblems multiple times. For instance, if you have 3 piles,
      |1| |4| |3|
      |5| |2| |6|
      and you took one coin from pile 1, and then 2 coins from pile 3, you know that taking 2 coins from pile 3 will get you +9 to your total.
      Well now what if you took 1 coin from pile 2 and then took 2 coins from pile 3? Pile 3 will still get you +9 to your current total. Since we already found this out earlier, it makes more sense to store this info in the cache.
      This is a very simple example though, imagine if the piles were much larger and you had already calculated the amount you'd get for pulling 25 coins from pile 12. It is much fast to just check "what happens when I have this many coins at pile 3?" than it is to calculate that amount multiple times.
      With caching, the time complexity is O(n*k).
      Without caching, the time complexity is O(k^n), an exponential time complexity which is always best to avoid if possible.
      Caching (memoization) is very helpful in dynamic programming problems and you might have used it before without realizing it. It is a small space complexity tradeoff for a big time complexity pay off.

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

    was able to come up with a memoized solution but TLE after passing 77 test cases :(... Converting memoized code to tabulated code is certainly tricky... Any advice on the same from folks here?

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

      int maxValueOfCoins(vector& piles, int k) {
      int n = piles.size();
      vector dp(k + 1, 0);
      vector temp(k + 1, 0);
      for (int i = n - 1; i >= 0; i--) {
      vector &pile = piles[i];
      int size = pile.size();
      for (int coins = 1; coins

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

      Once you got the recursive, memoized version written down clearly, it ain't that bad.
      First, let's look at the parameters we got: i, coins. Start by filling in all the base cases in your dp array. In this problem, the only base case is for i == n, thus we can set dp[n][k] = 0 for all possible values of k.
      Next, simply follow the places where you update your memo array/hashmap. On line 15, we have "dp[i][coins] = dfs(i + 1, coins)" which would translate into "dp[i][coins] = dp[i + 1][coins]". This might already give the hint that we need to fill the dp-array in decreasing order of parameter i (since dp[i][k] depends on dp[i + 1][k]).
      On line 19, we have "dp[i][coins] = max(dp[i][coins], curPile + dfs(i + 1, coins - j - 1)" which translates into
      "dp[i][coins] = max(dp[i][coins], curPile + dp[i + 1][coins - j - 1]". Again, we can see that dp[i][coins] depends on dp[i + 1][coins - j - 1], where coins - j - 1 < coins. So it seems like the second parameter, coins, needs to be filled in increasing order!
      So, if we ensure that we always compute dp[i + 1][k] before dp[i][k], then we're good. Likewise, we need to make sure that we first compute the result for 0 coins, then 1 coin, 2 coins, etc.
      The hard thing about tabulation is that we need to carefully think about the parameters dependencies, while in the recursive version it is "hidden" behind the more intuitive recursive calls.
      It might look something like this:
      n = len(piles)
      dp = [[-1] * (k + 1) for _ in range(n)]
      # base cases
      for k in range(k + 1):
      dp[n][k] = 0
      for i in range(n - 1, 0, -1):
      for k in range(k + 1):
      dp[i][k] = dp[i + 1][k]
      curPile = 0
      for j in range(min(coins, len(piles[i]))):
      curPile += piles[i][j]
      dp[i][k] = max(dp[i][k], curPile + dp[i + 1][k - j - 1])

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

    Why can't we use a heap to greedily pick up the max value ? In that case klog(n) right?

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

      I think the first example shows that a heap solution won't work (proof by contradiction)

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

      We shouldn't add all the elements from a pile. Just the topmost element of every pile. In the heap, we can store the (value, index of pile) as the entry

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

      @@lakshmanvengadesan9096 It will not return the correct answer. Like if you consider the given example, It will compare 1 and 7 and picks 7, and then it will compare 1 and 8, it will pick 8. so it return 15 which is wrong. We have to consider all the combinations.

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

      ​@@tanish5662 cool, i see the point now

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

    if I write this inside loop
    take = Math.max(sum+helper(i+1,k-j-1,piles,dp),take);
    why I have to take max with take itself..

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

      because you are taking max till now will choosing till now coins inside piles and and moving next piles selecting their coins or not both

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

    I don't even understand what the problem is asking >.

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

    Aliens 👽 👽 attendance taken by here ⊂⁠(⁠◉⁠‿⁠◉⁠)⁠つ

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

    Not too sure why we we had right node of parent 0 to be 0. Why can’t it be 7 from second pile?

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

    hey man I hope everything is alright with you , your keystrokes are concerning. Take care man. Probably my assumption is wrong

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

    Did you figure it out by yourself or saw the solution?

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

      He is not god of LeetCode.

    • @Box-iq1zj
      @Box-iq1zj 3 วันที่ผ่านมา

      @@shavvasatya7131 are u sure?