Leetcode Problem 2218. Maximum Value of K Coins From Piles

2218. Maximum Value of K Coins From Piles

Leetcode Solutions

Bottom-up Dynamic Programming

Algorithm

  1. Initialize a DP table dp with dimensions [n + 1][k + 1] and fill it with zeros.
  2. Iterate over the piles i from 1 to n.
    • For each pile, iterate over the number of coins coins from 0 to k.
      • Initialize currentSum to 0.
      • Iterate over the number of coins currentCoins from 0 to min(piles[i - 1].length, coins).
        • If currentCoins > 0, increase currentSum by the value of the coin at piles[i - 1][currentCoins - 1].
        • Update dp[i][coins] to the maximum of its current value and dp[i - 1][coins - currentCoins] + currentSum.
  3. Return the value at dp[n][k] as the maximum total value of coins that can be taken.
UML Thumbnail

Top-Down Dynamic Programming (Memoization)

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...