Maximum Value of K Coins from Piles

Hard Topics Company Tags

Each list in piles describes a nonempty stack of coins from top to bottom. In one move, remove the top coin from any nonempty pile and add its value to your total. Return the largest total value obtainable by removing exactly k coins. A coin deeper in a pile is available only after all coins above it have been removed.

Example 1:

Maximum Value of K Coins From Piles, example 1: illustration of the example

Input: piles = [[2,20],[10]]
k = 2

Output: 22

Explanation: Take the first two coins from the first pile for a total of 2 + 20 = 22. Taking one coin from each pile gives only 12.

Example 2:

Maximum Value of K Coins From Piles, example 2: illustration of the example

Input: piles = [[5],[7,1]]
k = 3

Output: 13

Explanation: Exactly three coins must be taken, and there are three available. Their values sum to 5 + 7 + 1 = 13.

Example 3:

Maximum Value of K Coins From Piles, example 3: illustration of the example

Input: piles = [[1,100],[9]]
k = 1

Output: 9

Explanation: Only the top coins, 1 and 9, are immediately available. Choose 9; reaching the coin worth 100 would require two removals.

Constraints:

  • 1 <= piles.length <= 1000
  • Each pile is nonempty.
  • 1 <= piles[i][j] <= 100000
  • 1 <= k <= the total number of coins <= 2000.


Topics

Company Tags

Please upgrade to NeetCode Pro to view company tags.