Best Time to Buy and Sell Stock III
Question
https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
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 two transactions.
Example
Given an example [4,4,6,1,1,4,2,5], return 6.
Analysis
一种算法比较容易想到: 找寻一个点j,将原来的prices[0..n-1]分割为prices[0..j]和prices[j..n-1],分别求两段的最大profit。 内层循环与Best Time to Buy and Sell Stock相同,为O(n), 外层循环为O(n),记录不同的j两段maxProfit的值需要O(n)空间,因此总体时间复杂度为O(n^2),空间复杂度为O(n)。
改进(参考pickless的讲解): 对于点j+1,求prices[0..j+1]的最大profit时,很多工作是重复的,在求prices[0..j]的maxProfit中已经计算过了。 类似于Best Time to Buy and Sell Stock,可以在O(1)的时间从prices[0..j]推出prices[0..j+1]的最大profit。 但是如何从prices[j..n-1]推出prices[j+1..n-1]?反过来思考,我们可以用O(1)的时间由prices[j+1..n-1]推出prices[j..n-1]。 最终算法: 数组l[i]记录了prices[0..i]的最大profit, 数组r[i]记录了prices[i..n]的最大profit。 已知l[i],求l[i+1]是简单的,同样已知r[i],求r[i-1]也很容易。 最后,我们再用O(n)的时间找出最大的l[i]+r[i],即为题目所求。 这样时间复杂度O(n), 空间复杂度O(n)
参考 http://blog.csdn.net/fightforyourdream/article/details/14503469
状态机State Machine:
这种想法很神奇:
// Assume we are in state S
// If we buy, we are spending money but we can also choose to do nothing
// Doing nothing means going from S->S
// Buying means going from some state X->S, losing some money in the process
S = max(S, X-prices[i])
// Similarly, for selling a stock
S = max(S, X+prices[i])Java Implementation
Solution
A Intuitive Implementation Using Divide And Conquer
Another Implementation with Dynamic Programming
State Machine
Reference
http://liangjiabin.com/blog/2015/04/leetcode-best-time-to-buy-and-sell-stock.html http://www.jiuzhang.com/solutions/best-time-to-buy-and-sell-stock-iii/
Last updated
Was this helpful?