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.
这题标准解法还是用2维dp做,对于每天定义4个变量,第一次买/卖,第二次买/卖.他们之间的递推关系也很容易推出(见代码).然后由于i
状态只取决于i-1
状态,可以将空间复杂度降低到O(n)
(1维dp).
1 | class Solution { |
Even better, 我们可以只用4个变量(或者1维数组)来求解.1
2
3
4
5
6
7
8
9
10
11
12
13public class Solution {
int maxProfit(vector<int>& prices) {
int hold1 = INT_MIN, hold2 = INT_MIN;
int release1 = 0, release2 = 0;
for(int i : prices){
release2 = max(release2, hold2+i); // The maximum if we've just sold 2nd stock so far.
hold2 = max(hold2, release1-i); // The maximum if we've just buy 2nd stock so far.
release1 = max(release1, hold1+i); // The maximum if we've just sold 1nd stock so far.
hold1 = max(hold1, -i); // The maximum if we've just buy 1st stock so far.
}
return release2; ///Since release1 is initiated as 0, so release2 will always higher than release1.
}
}