2218. Maximum Value of K Coins From Piles

It is a hidden 01 knapsack problem. The elements are in the stack, which we can only take it one by one from the top, makes it little hard to do the knapsack.

However, we can preprocess the data, making the pile to be a prefix sum pile, which we can get the total coin sum in O(1) time complexity.

After being able to get the taking j element in the stack in O(1), the rest of things are treating k as a weight, and sum of (0 ~ jth element in ith pile) as a value.

we have dp[j] representing the best value of taking j the elements.

the j should start from min( total pile.length() , k ) to 0, here I won't explain why the j should iterate reversely, this is a simple concept of 01 knapsack space compression. 

dp[j] = max(dp[j], dp[j - k] + v_i)

class Solution {
public:
    int maxValueOfCoins(vector<vector<int>>& piles, int k) {
        int nSum = 0;
        int dp[k + 1];
        //preprocess
        memset(dp, 0, sizeof(dp));
        for(auto &pile : piles){
            int n = pile.size();
            for(int i = 1; i < n; i++){
                pile[i] += pile[i - 1];
            }
        }
        for(auto pile : piles){
            int n = pile.size();
            nSum = min(nSum + n, k);//if the total length<k,you cant take k element
            for(int j = nSum; j >= 0; j--){
                //try all the new value in new pile to see if there are greater val
                for(int k = 0; k < min(n, j); k++){
                    //pile[0] has 1 weight and pile[1] has 2weights
                    //so dp[j - (k + 1)]
                    dp[j] = max(dp[j], dp[j - k - 1] + pile[k]);
                }
            }
        }
        return dp[k];
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值