[leetcode] 188. Best Time to Buy and Sell Stock IV

Say you have an array for which theith element is the price of a given stock on dayi.

Design an algorithm to find the maximum profit. You may complete at mostk transactions.

Note:

You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).

这道题仍旧和股票相关,是第121、122、123三题的汇总,把交易次数变成了最多k次,难度为Hard。

每一天的操作分三种情况,不操作、买入或卖出,不操作的情况最大收益不变。初始收益为0,买入时收益减买入价格,卖出时收益加卖出价格。如果没有前面三题的基础,这道题还是挺难的,不过通过第121和第123题的一步步深入,我们能够水到渠成地想出这道题的状态机迁移策略。每天的状态变为:

  • ……

  • 买入第m次,没有卖出

  • 卖出第m次,没有买入

  • 买入第m+1次,没有卖出

  • 卖出第m+1次,没有买入

  • ……

由此便可以依照第123题完成这道题。

但提交之后,在k非常大的时候报了超时,因而在每天的状态变更中依据k进行迭代可能存在问题。看了一下报错的k,貌似是10亿次。。测试的天数有这么大吗?如果只有10天的股票价格,能够操作5次以上吗?所以在操作次数大于天数的一半时是不能操作那么多次的,就相当于不限操作次数,这不正是第122题嘛。因而我们对这种情况按照第122题的方式用贪婪算法进行预先处理。(这部分没有想到的话略坑。。看了别人的方法才知道错在了哪里。)

具体代码:

class Solution {
public:
    int maxProfit(int k, vector<int>& prices) {
        if(k <= 0) return 0;
        
        vector<int> profit;
        int maxPf = 0;
        
        if(k > prices.size()/2) {
            for(int i=1; i<prices.size(); i++)
                maxPf += max(prices[i]-prices[i-1], 0);
            return maxPf;
        }
        
        for(int i=0; i<k; i++) {
            profit.push_back(INT_MIN);
            profit.push_back(0);
        }
        
        for(int i=0; i<prices.size(); i++) {
            for(int j=2*k-1; j>1; j-=2) {
                profit[j] = max(profit[j], profit[j-1]+prices[i]);
                profit[j-1] = max(profit[j-1], profit[j-2]-prices[i]);
            }
            profit[1] = max(profit[1], profit[0]+prices[i]);
            profit[0] = max(profit[0], -prices[i]);
        }
        
        for(int i=1; i<profit.size(); i+=2) {
            maxPf = max(maxPf, profit[i]);
        }
        
        return maxPf;
    }
};

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值