[LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
来源:互联网 发布:粤贵银交易软件下载 编辑:程序博客网 时间:2024/03/28 21:33
转自:http://www.cnblogs.com/grandyang/p/4295761.html
http://blog.csdn.net/linhuanmars/article/details/23236995 这里提到的,深有同感:
不禁感叹算法真是牛逼哈,这么个复杂生活问题几行代码就解决了。
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.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
Credits:
Special thanks to @Freezen for adding this problem and creating all test cases.
这道题实际上是之前那道 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的差值,和前一天的局部最优加上差值后相比,两者之中取较大值,而全局最优比较局部最优和前一天的全局最优。
(第一个是全局到i-1天进行j-1次交易,然后加上今天的交易,如果今天是赚钱的话(也就是前面只要j-1次交易,最后一次交易取当前天),第二个量则是取local第i-1天j次交易,然后加上今天的差值(这里因为local[i-1][j]原来包含第i-1天卖出的交易,也可以变成第i天卖出,并不会增加交易次数,而且这里无论diff是不是大于0都一定要加上,因为否则就不满足local[i][j]必须在最后一天卖出的条件了)。)
但这道题还有个坑,就是如果k的值远大于prices的天数,比如k是好几百万,而prices的天数就为若干天的话,上面的DP解法就非常的没有效率,应该直接用Best Time to Buy and Sell Stock II 买股票的最佳时间之二的方法来求解,所以实际上这道题是之前的二和三的综合体,代码如下:
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; }};
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- LeetCode OJ 之 Best Time to Buy and Sell Stock (买卖股票的最佳时间)
- LeetCode-121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)
- **[Lintcode]Best Time to Buy and Sell Stock IV 买卖股票的最佳时机 IV Leetcode
- LeetCode OJ 之 Best Time to Buy and Sell Stock II (买卖股票的最佳时间 2)
- LeetCode OJ 之 Best Time to Buy and Sell Stock III (买卖股票的最佳时间 - 三)
- Best Time to Buy and Sell Stock 【最佳买卖股票的时间】【容易】
- 122. Best Time to Buy and Sell Stock II 最佳买卖股票的时间
- 【LeetCode-面试算法经典-Java实现】【121-Best Time to Buy and Sell Stock(最佳买卖股票的时间)】
- 121. Best Time to Buy and Sell Stock 最佳买卖股票时间
- Best Time to Buy and Sell Stock II 最佳时间买入卖出股票(多次买卖)@LeetCode
- Best Time to Buy and Sell Stock III 最佳时间买入卖出股票(最多两次买卖)@LeetCode
- LeetCode 121 Best Time to Buy and Sell Stock(股票买入卖出的最佳时间)
- [LeetCode]122. Best Time to Buy and Sell Stock II(最佳买卖时间 II)
- Best Time to Buy and Sell Stock 买卖股票最好的时间
- Best Time to Buy and Sell Stock II 买卖股票最好的时间II
- Best Time to Buy and Sell Stock III 买卖股票的最好时间
- Best Time to Buy and Sell Stock 买卖股票的最好时间
- 并发面试题
- http://www.cnblogs.com/elleniou/archive/2012/07/31/2617312.html
- Java_集合操作_多种最值算法,适时选择
- 哈理工 oj——神奇的进制转换
- 中国大牛市是一场世界大戏 (转载)
- [LeetCode] Best Time to Buy and Sell Stock IV 买卖股票的最佳时间之四
- C++_使用C++代码进行MD5加密
- webview与js交互 问题一
- 图解 Panda3D引擎开发入门
- JAVA:数组
- hdu 5203 Rikka with wood sticks (暴力枚举边)
- linux设备驱动第五篇:驱动中的并发与竟态
- Java:比较运算符compareTo()、equals()、==之间的区别与应用总结
- bash命令提示行设置