Hard-题目38:188. Best Time to Buy and Sell Stock IV

转载 2016年05月31日 23:48:23

题目原文:
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.
题目大意:
又是买卖股票。这次进一步延伸,你至多只能进行k次交易。(买和卖都算一次)
题目分析:
又是直接不会。摘抄Grandyang的微博如下:
这道题实际上是之前那道 Best Time to Buy and Sell Stock III 买股票的最佳时间之三的一般情况的推广,还是需要用动态规划Dynamic programming来解决,具体思路如下:

这里我们需要两个递推公式来分别更新两个变量local和global,参见网友Code Ganker的博客,我们其实可以求至少k次交易的最大利润。我们定义local[i][j]为在到达第i天时最多可进行j次交易并且最后一次交易在最后一天卖出的最大利润,此为局部最优。然后我们定义global[i][j]为在到达第i天时最多可进行j次交易的最大利润,此为全局最优。它们的递推式为:

local[i][j] = max(global[i - 1][j - 1] + max(diff, 0), local[i - 1][j] + diff)

global[i][j] = max(local[i][j], global[i - 1][j])

其中局部最优值是比较前一天并少交易一次的全局最优加上大于0的差值,和前一天的局部最优加上差值后相比,两者之中取较大值,而全局最优比较局部最优和前一天的全局最优。

但这道题还有个坑,就是如果k的值远大于prices的天数,比如k是好几百万,而prices的天数就为若干天的话,上面的DP解法就非常的没有效率,应该直接用Best Time to Buy and Sell Stock II 买股票的最佳时间之二的方法来求解,所以实际上这道题是之前的二和三的综合体,
源码:(language:cpp)

class Solution {
public:
    int maxProfit(int k, vector<int> &prices) {
        if (prices.empty()) return 0;
      //  if (k >= prices.size()) return solveMaxProfit(prices);
        int g[k + 1] = {0};
        int l[k + 1] = {0};
        for (int i = 0; i < prices.size() - 1; ++i) {
            int diff = prices[i + 1] - prices[i];
            for (int j = k; j >= 1; --j) {
                l[j] = max(g[j - 1] + max(diff, 0), l[j] + diff);
                g[j] = max(g[j], l[j]);
            }
        }
        return g[k];
    }
    int solveMaxProfit(vector<int> &prices) {
        int res = 0;
        for (int i = 1; i < prices.size(); ++i) {
            if (prices[i] - prices[i - 1] > 0) {
                res += prices[i] - prices[i - 1];
            }
        }
        return res;
    }
};

成绩:
8ms,63.62%,12ms,41.65%

相关文章推荐

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
  • 258

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...

leetcode_188 Best Time to Buy and Sell Stock IV

题目分析: 用一个数组表示股票每天的价格,数组中第i个数表示股票在第i天的价格。最多可以交易k次,但手上只能持有一支股票,求此时的最大收益。 解题思路: 动态规划求解1)如果交易次数大于股票变化的天数...

[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. ...

【leetcode】188. Best Time to Buy and Sell Stock IV

Difficulty:Hard Say you have an array for which the ith element is the price of a given stock on ...

Leetcode 188. Best Time to Buy and Sell Stock IV

188. Best Time to Buy and Sell Stock IV Total Accepted: 34269 Total Submissions: 146381 Difficulty...

[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...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Hard-题目38:188. Best Time to Buy and Sell Stock IV
举报原因:
原因补充:

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