#2218
Maximum Value of K Coins From Piles
expert · 1165 · lc hard +32 · verified · 60.4% accepted · 2,442 likes · top 59%
Description
There are n coin stacks on a table. Each stack holds one or more coins of varying denominations, and you may only take from the top.
On each move, pick the topmost coin from any stack, remove it, and add it to your wallet.
You are given a list piles where piles[i] lists the coin values in the ith stack from top to bottom, along with a positive integer k. Return the highest total wallet value achievable by selecting exactly k coins in an optimal order.
Example 1:
Input: piles = [[1,100,3],[7,8,9]], k = 2
Output: 101
Explanation:
The above diagram shows the different ways we can choose k coins.
The maximum total we can obtain is 101.
Example 2:
Input: piles = [[100],[100],[100],[100],[100],[100],[1,1,1,1,1,1,700]], k = 7
Output: 706
Explanation:
The maximum total can be obtained if we choose all coins from the last pile.
Code
1
2
3