Leetcode 188. Best Time to Buy and Sell Stock IV
来源:互联网 发布:adobe 2018套装软件 编辑:程序博客网 时间:2024/06/05 11:33
188. Best Time to Buy and Sell Stock IV
Total Accepted: 34269 Total Submissions: 146381 Difficulty: Hard
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.
Hide Similar Problems
思路:
我们还是使用“局部最优和全局最优解法”。我们维护两种量,一个是当前到达第i天可以最多进行j次交易,最好的利润是多少(global[i][j]),另一个是当前到达第i天,最多可进行j次交易,并且最后一次交易在当天卖出的最好的利润是多少(local[i][j])下面我们来看递推式,全局的比较简单,global[i][j]=max(local[i][j],global[i-1][j]),也就是去当前局部最好的,和过往全局最好的中大的那个(因为最后一次交易如果包含当前天一定在局部最好的里面,否则一定在过往全局最优的里面)。全局(到达第i天进行j次交易的最大收益) = max{局部(在第i天交易后,恰好满足j次交易),全局(到达第j-1天时已经满足j次交易)}对于局部变量的维护,递推式是local[i][j]=max(global[i-1][j-1]+max(diff,0),local[i-1][j]+diff),也就是看两个量,第一个是全局到i-1天进行j-1次交易,然后加上今天的交易,如果今天是赚钱的话(也就是前面只要j-1次交易,最后一次交易取当前天),第二个量则是取local第i-1天j次交易,然后加上今天的差值(这里因为local[i-1][j]比如包含第i-1天卖出的交易,所以现在变成第i天卖出,并不会增加交易次数,而且这里无论diff是不是大于0都一定要加上,因为否则就不满足local[i][j]必须在最后一天卖出的条件了)。局部(在第i天交易后,总共交易了j次) = max{情况2,情况1}情况1:在第i-1天时,恰好已经交易了j次(local[i-1][j]),那么如果i-1天到i天再交易一次:即在第i-1天买入,第i天卖出(diff),则这不并不会增加交易次数!【例如我在第一天买入,第二天卖出;然后第二天又买入,第三天再卖出的行为 和 第一天买入,第三天卖出 的效果是一样的,其实只进行了一次交易!因为有连续性】情况2:第i-1天后,共交易了j-1次(global[i-1][j-1]),因此为了满足“第i天过后共进行了j次交易,且第i天必须进行交易”的条件:我们可以选择1:在第i-1天买入,然后再第i天卖出(diff),或者选择在第i天买入,然后同样在第i天卖出(收益为0)。上面的算法中对于天数需要一次扫描,而每次要对交易次数进行递推式求解,所以时间复杂度是O(n*k),如果是最多进行两次交易,那么复杂度还是O(n)。空间上只需要维护当天数据皆可以,所以是O(k),当k=2,则是O(1)。
重复一遍:
local[i][j]:第i天完成第j次交易。
local[i][j] = Math.max(global[i - 1][j - 1] + Math.max(diff, 0), local[i - 1][j] + diff); // 可以同天买卖;最后一天卖了今天买卖那么可以归并为一次。
global[i][j] = Math.max(global[i - 1][j], local[i][j]);
public class Solution { public int maxProfit(int k, int[] prices) { if (prices.length < 2) return 0; int days = prices.length; if (k >= days) return maxProfit2(prices); int[][] local = new int[days][k + 1]; int[][] global = new int[days][k + 1]; for (int i = 1; i < days ; i++) { int diff = prices[i] - prices[i - 1]; for (int j = 1; j <= k; j++) { // local[i][j] = Math.max(global[i - 1][j - 1] + Math.max(diff, 0), local[i - 1][j] + diff); global[i][j] = Math.max(global[i - 1][j], local[i][j]); } } return global[days - 1][k]; } public int maxProfit2(int[] prices) { int maxProfit = 0; for (int i = 1; i < prices.length; i++) { if (prices[i] > prices[i - 1]) { maxProfit += prices[i] - prices[i - 1]; } } return maxProfit; }}
0 0
- [leetcode] 188.Best Time to Buy and Sell Stock IV
- [leetcode] 188.Best Time to Buy and Sell Stock IV
- [leetcode] 188. Best Time to Buy and Sell Stock IV
- [LeetCode] 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- 【leetcode】188. Best Time to Buy and Sell Stock IV
- LeetCode 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- Leetcode 188. Best Time to Buy and Sell Stock IV
- LeetCode 188. Best Time to Buy and Sell Stock IV
- [LeetCode] Best Time to Buy and Sell Stock IV
- leetcode: Best Time to Buy and Sell Stock IV
- LeetCode(188) Best Time to Buy and Sell Stock IV
- Best Time to Buy and Sell Stock IV -- leetcode
- Best Time to Buy and Sell Stock IV--LeetCode
- LeetCode : Best Time to Buy and Sell Stock III & IV
- Best Time to Buy and Sell Stock IV Leetcode Java
- 左程云代码面试指南:仅使用递归函数和栈实现一个逆序栈
- python学习(一)
- uva 170
- Putty基础教程之(一).入门命令学习及编写HTML
- 用Construct 2制作一个升级版的坦克大战
- Leetcode 188. Best Time to Buy and Sell Stock IV
- CSS中的渐变效果
- 95.Validate Binary Search Tree-验证二叉查找树(中等题)
- 斐波那契数列
- code2
- 分布式一致性原理学习笔记(3)
- HDOJ 5929 Basic Data Structure 【2016CCPC东北地区赛】STL
- 如果你的电脑中有几个xcode版本
- 关于面向“专家”的实用技术与面向“大众”的普及技术