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 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. 给你一个数组...
  • will130
  • will130
  • 2015年05月29日 13:36
  • 265

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

题目:https://oj.leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/

LeetCode(188) Best Time to Buy and Sell Stock IV (Java)

题目如下: Best Time to Buy and Sell Stock Total Accepted: 43912 Total Submissions: 135635 My Submissions...

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_[python/C++]_121/122/123/188.Best Time to Buy and Sell Stock I/II/III/IV

121. Best Time to Buy and Sell Stock [题目] Say you have an array for which the ith element is the p...

Best Time to Buy and Sell Stock IV-- LeetCode

这里我们先解释最多可以进行k次交易的算法,然后最多进行两次我们只需要把k取成2即可。我们还是使用“局部最优和全局最优解法”。我们维护两种量,一个是当前到达第i天可以最多进行j次交易,最好的利润是多少(...

leetcode -- Best Time to Buy and Sell Stock IV --难点要看

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/参考: http://bookshadow.com/weblog/2...
  • xyqzki
  • xyqzki
  • 2015年12月28日 18:34
  • 286

Leetcode227: 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 第188题,我的解法,Best Time to Buy and Sell Stock IV

leetcode第188题,Best Time to Buy and Sell Stock IV题目如下: https://leetcode.com/problems/best-time-to-buy...

LeetCode Best Time to Buy and Sell Stock IV

Description: Say you have an array for which the ith element is the price of a given stock on day ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Best Time to Buy and Sell Stock IV
举报原因:
原因补充:

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