123 Best Time to Buy and Sell Stock III 買賣股票的最佳時機(jī) III
Description:
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.
Note:
You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).
Example:
Example 1:
Input: [3,3,5,0,0,3,1,4]
Output: 6
Explanation: Buy on day 4 (price = 0) and sell on day 6 (price = 3), profit = 3-0 = 3.
Then buy on day 7 (price = 1) and sell on day 8 (price = 4), profit = 4-1 = 3.
Example 2:
Input: [1,2,3,4,5]
Output: 4
Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
engaging multiple transactions at the same time. You must sell before buying again.
Example 3:
Input: [7,6,4,3,1]
Output: 0
Explanation: In this case, no transaction is done, i.e. max profit = 0.
題目描述:
給定一個數(shù)組,它的第 i 個元素是一支給定的股票在第 i 天的價格。
設(shè)計一個算法來計算你所能獲取的最大利潤。你最多可以完成 兩筆 交易。
注意:
你不能同時參與多筆交易(你必須在再次購買前出售掉之前的股票)。
示例 :
示例 1:
輸入: [3,3,5,0,0,3,1,4]
輸出: 6
解釋: 在第 4 天(股票價格 = 0)的時候買入,在第 6 天(股票價格 = 3)的時候賣出,這筆交易所能獲得利潤 = 3-0 = 3 。
隨后,在第 7 天(股票價格 = 1)的時候買入,在第 8 天 (股票價格 = 4)的時候賣出,這筆交易所能獲得利潤 = 4-1 = 3 。
示例 2:
輸入: [1,2,3,4,5]
輸出: 4
解釋: 在第 1 天(股票價格 = 1)的時候買入,在第 5 天 (股票價格 = 5)的時候賣出, 這筆交易所能獲得利潤 = 5-1 = 4 。
注意你不能在第 1 天和第 2 天接連購買股票,之后再將它們賣出。
因為這樣屬于同時參與了多筆交易,你必須在再次購買前出售掉之前的股票。
示例 3:
輸入: [7,6,4,3,1]
輸出: 0
解釋: 在這個情況下, 沒有交易完成, 所以最大利潤為 0。
思路:
動態(tài)規(guī)劃
dp[i][k][b]表示在第 i天還有 k次購買機(jī)會, b為 bool量表示是否持有股票
如 dp[2][3][1]表示第二天, 還有 3次購買機(jī)會, 手上持有股票; dp[1][1][0]表示第一天, 還有 1次操作機(jī)會, 手上沒有股票
轉(zhuǎn)移方程:
dp[i][k][0] = max(dp[i - 1][k][0], dp[i - 1][k][1] + price)
dp[i][k][1] = max(dp[i - 1][k][1], dp[i - 1][k - 1][0] - price)
賣出股票就加上今天的售價, 買入股票就減去今天的售價
初始條件(base):
dp[-1][k][0] = 0, 即還沒開始買入股票之前收益是 0
dp[-1][k][1] = -inf, 即不可能在還沒開始買入股票之前就持有股票
dp[i][0][0] = 0, 如果沒有購買機(jī)會, 那收益就是 0
dp[i][0][0] = -inf, 如果沒有購買機(jī)會, 不能持有股票
在這道題, k = 2, 可以窮舉所有狀態(tài), 可以將空間壓縮到 O(1)
時間復(fù)雜度O(n), 空間復(fù)雜度O(1)
代碼:
C++:
class Solution
{
public:
int maxProfit(vector<int>& prices)
{
int dp_i10 = 0, dp_i11 = INT_MIN, dp_i20 = 0, dp_i21 = INT_MIN;
for (auto price : prices)
{
dp_i20 = max(dp_i20, dp_i21 + price);
dp_i21 = max(dp_i21, dp_i10 - price);
dp_i10 = max(dp_i10, dp_i11 + price);
dp_i11 = max(dp_i11, -price);
}
return dp_i20;
}
};
Java:
class Solution {
public int maxProfit(int[] prices) {
int dp_i10 = 0, dp_i11 = Integer.MIN_VALUE, dp_i20 = 0, dp_i21 = Integer.MIN_VALUE;
for (int price : prices) {
dp_i20 = Math.max(dp_i20, dp_i21 + price);
dp_i21 = Math.max(dp_i21, dp_i10 - price);
dp_i10 = Math.max(dp_i10, dp_i11 + price);
dp_i11 = Math.max(dp_i11, -price);
}
return dp_i20;
}
}
Python:
class Solution:
def maxProfit(self, prices: List[int]) -> int:
dp_i10, dp_i11, dp_i20, dp_i21 = 0, -float('inf'), 0, -float('inf')
for price in prices:
dp_i20, dp_i21, dp_i10, dp_i11 =max(dp_i20, dp_i21 + price), max(dp_i21, dp_i10 - price), max(dp_i10, dp_i11 + price), max(dp_i11, -price)
return dp_i20