This approach uses dynamic programming to find the maximum value of k coins from the given piles. We maintain a DP table where dp[i][j] represents the maximum value of coins we can obtain using the first i piles and j picks.
The main idea is to update this table by considering the top coin from the current pile, and deciding whether or not to pick it. We also keep track of cumulative sums for quicker reference.
Time Complexity: O(n * k * m), where n is the number of piles, k is the number of coins to pick, and m is the average number of coins in each pile.
Space Complexity: O(n * k)
1#include <vector>
2#include <algorithm>
3using namespace std;
4
5int maxValueOfCoins(vector<vector<int>>& piles, int k) {
6 int n = piles.size();
7 vector<vector<int>> dp(n + 1, vector<int>(k + 1, 0));
8
9 for (int i = 1; i <= n; ++i) {
10 vector<int>& current_pile = piles[i - 1];
11 int pile_sum = 0;
12 for (int j = 0; j < current_pile.size(); ++j) {
13 pile_sum += current_pile[j];
14 for (int x = k; x >= j + 1; --x) {
15 dp[i][x] = max(dp[i][x], dp[i - 1][x - (j + 1)] + pile_sum);
16 }
17 }
18 }
19
20 return dp[n][k];
21}
22
23// Example usage:
24// vector<vector<int>> piles = {{1, 100, 3}, {7, 8, 9}};
25// int k = 2;
26// int result = maxValueOfCoins(piles, k); // Output: 101
The C++ solution closely follows the logic of the Python solution using a 2D vector for the DP table initialization. It iteratively updates the DP table and uses cumulative sums to efficiently calculate the maximum coin values.
This approach involves greedy selection to achieve a near-optimal solution. The idea is to always select the coin with the highest value from a potential list of top coins from each pile, and repeat this k times.
This method might not guarantee the absolute optimal solution due to local-optimal greediness but can offer a reasonable approximation for certain constraints and datasets.
Time Complexity: O(k log n), as we perform k heap operations on n piles.
Space Complexity: O(n), due to storing piles in the heap.
1import java.util.*;
2
3public class Solution {
4 public int maxValueOfCoins(List<List<Integer>> piles, int k) {
5 PriorityQueue<int[]> maxHeap = new PriorityQueue<>((a, b) -> b[0] - a[0]);
6
7 for (int i = 0; i < piles.size(); i++) {
8 maxHeap.offer(new int[]{piles.get(i).get(0), 0, i});
9 }
10
11 int totalValue = 0;
12 for (int i = 0; i < k; i++) {
13 int[] top = maxHeap.poll();
14 totalValue += top[0];
15 int pileIndex = top[2], coinIndex = top[1];
16 if (coinIndex + 1 < piles.get(pileIndex).size()) {
17 maxHeap.offer(new int[]{piles.get(pileIndex).get(coinIndex + 1), coinIndex + 1, pileIndex});
18 }
19 }
20 return totalValue;
21 }
22
23 // Example usage:
24 // List<List<Integer>> piles = Arrays.asList(Arrays.asList(1, 100, 3), Arrays.asList(7, 8, 9));
25 // int k = 2;
26 // int result = new Solution().maxValueOfCoins(piles, k); // Output: 101
27}
The Java solution utilizes a PriorityQueue to represent a max-heap, ensuring the selection of the currently available highest value coin in each iteration.