Best Time to Buy and Sell Stock IV

原创 2015年07月10日 23:45:56

leetcode中的股票问题(4)

第四题还是有点难的,特别是在连续完成前3题后,思路可能就顿时局限了,主要参考别人的实现,我这里做一些分析。
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/

原题

Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most k transactions.
翻译:和前一题基本一样,只是从最多2手交易改成了最多k手交易。

问题分析

这里我的直觉就是用动态规划来解决这个问题,但是具体怎么做呢?
很容易想到的就是维护变量profit(i,j),这里ij表示在i天最多做j手交易。
profit(i,j)=Max(profit(i1,j),profitAt(i,j))
其中profitAt(i,j)表示,在第i天一定要发生交易的情况下最多做j手交易的最大收益(有点拗口,吼吼吼),因为这样才能对profit(i,j)做一个合理的划分。
profitAt(i,j)呢?怎么递推呢?
我们说profitAt(i,j)=Max(profit(i1,j1)+Max(0,diff),profitAt(i1,j)+diff),其中diff=price(i)price(i1)
怎么理解呢,同样是要做一个划分,既然第i天必定有交易(必定有卖出行为),那我对这一手的买进时间做这样划分:买进时间>=i1或者<i1,对于>=i1的情况,在i1天之前就完成j1手交易,而最后一次买卖的收益是Max(0,diff);对于<i1的情况,我们完全可以用profitAt(i1,j)+diff)来递推,直接加上diff的原因是将原本在第i1天卖掉的股票移到第i天卖掉,直接加上这两天的差价就行!
然后就很简单了,直接上代码。
下面的代码中 global就是profit()函数,而local就是profitAt()函数。

代码

public int maxProfit(int k, int[] prices) {
    if(prices==null || prices.length==0)
            return 0;
    //如果k>一半天数,问题退化成问题(3)了
    if(k > prices.length/2)
    {
        int sum =0;
        for(int i = 0 ; i < prices.length-1 ; i++)
        {
            int gain = prices[i+1] - prices[i];
            if(gain > 0)
                sum += gain;
        }
        return sum;
    }

    int[][] global=new int[prices.length][k+1];
    int[][] local=new int[prices.length][k+1];
    for(int i=0;i<prices.length-1;i++)
    {
        int diff=prices[i+1]-prices[i];
        for(int j=0;j<=k-1;j++)
        {
            local[i+1][j+1]=Math.max(global[i][j]+Math.max(diff,0),local[i][j+1]+diff);
            global[i+1][j+1]=Math.max(global[i][j+1],local[i+1][j+1]);
        }
    }
    return global[prices.length-1][k];
}

接下来我们会去看看在leetcode中和数组以及链表的“rotate”操作相关的几个问题~

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode Best Time to Buy and Sell Stock IV

Leetcode Best Time to Buy and Sell Stock IV,本题主要是找出最大值与买卖次数的关系,很快可以求得结果。卖买次数与k的关系分析 首先我们知道当次数不限时,只需要...

【LeetCode】Best Time to Buy and Sell Stock IV 动态规划dp解法(C++)

乍看此题有难度,实则解法有规律。

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

题目: Say you have an array for which the ith element is the price of a given stock on day i.Design a...

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

Say you have an array for which the ith element is the price of a given stock on day i. Design an...

[LeetCode][Java] Best Time to Buy and Sell Stock IV

题目: Say you have an array for which the ith element is the price of a given stock on day i. ...

121.122.123.188. Best Time to Buy and Sell Stock I II III IV

Say you have an array for which the ith element is the price of a given stock on day i. If you were...

<LeetCode OJ> 123. / 188. Best Time to Buy and Sell Stock (III / IV)

123. Best Time to Buy and Sell Stock III My Submissions Question Total Accepted: 49478 Total Sub...

Best Time to Buy and Sell Stock IV

题目Say you have an array for which the ith element is the price of a given stock on day i.Design an a...

188 Best Time to Buy and Sell Stock IV [Leetcode]

题目内容: Say you have an array for which the ith element is the price of a given stock on day i. Desi...

LeetCode--Best Time to Buy and Sell Stock IV(DP + 滚动数组)

题目:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)