本文地址:https://leetcode-cn.com/circle/article/qiAgHn/
原文出處:https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/discuss/108870/Most-consistent-ways-of-dealing-with-the-series-of-stock-problems
大家好,我是吳師兄,今天給大家分享一篇關於股票問題系列的文章,掌握後輕輕鬆鬆秒殺 LeetCode 上所有的股票問題。
本文的作者爲 Storm,目前在力扣上全站排名第一,已獲作者授權轉載此文,希望對你刷題有幫助。
前言
此文爲轉載翻譯,和原文相比,這篇文章多了未優化空間的代碼,且代碼都重新寫了,另外更改了部分文字描述。
股票問題一共有六道題:
-
121、買賣股票的最佳時機 -
122、買賣股票的最佳時機 II -
123、買賣股票的最佳時機 III -
188、買賣股票的最佳時機 IV -
309、最佳買賣股票時機含冷凍期 -
714、買賣股票的最佳時機含手續費
每個問題都有優質的題解,但是大多數題解沒有建立起這些問題之間的聯繫,也沒有給出股票問題系列的通解。
這篇文章給出適用於全部股票問題的通解,以及對於每個特定問題的特解。
一、通用情況
這個想法基於如下問題:給定一個表示每天股票價格的數組,什麼因素決定了可以獲得的最大收益?
相信大多數人可以很快給出答案,例如「在哪些天進行交易以及允許多少次交易」。
這些因素當然重要,在問題描述中也有這些因素。
然而還有一個隱藏但是關鍵的因素決定了最大收益,下文將闡述這一點。
首先介紹一些符號:
-
用 n 表示股票價格數組的長度; -
用 i 表示第 i 天( i 的取值範圍是 0 到 n - 1); -
用 k 表示允許的最大交易次數; -
用 T[i][k] 表示在第 i 天結束時,最多進行 k 次交易的情況下可以獲得的最大收益。
基準情況是顯而易見的:T[-1][k] = T[i][0] = 0
,表示沒有進行股票交易時沒有收益(注意第一天對應 i = 0
,因此 i = -1
表示沒有股票交易)。
現在如果可以將 T[i][k]
關聯到子問題,例如 T[i - 1][k]
、T[i][k - 1]
、T[i - 1][k - 1]
等子問題,就能得到狀態轉移方程,並對這個問題求解。
如何得到狀態轉移方程呢?
最直接的辦法是看第 i 天可能的操作。有多少個選項?答案是三個:買入、賣出、休息。
應該選擇哪個操作?
答案是:並不知道哪個操作是最好的,但是可以通過計算得到選擇每個操作可以得到的最大收益。
假設沒有別的限制條件,則可以嘗試每一種操作,並選擇可以最大化收益的一種操作。
但是,題目中確實有限制條件,規定不能同時進行多次交易,因此如果決定在第 i 天買入,在買入之前必須持有 0 份股票,如果決定在第 i 天賣出,在賣出之前必須恰好持有 1 份股票。
持有股票的數量是上文提及到的隱藏因素,該因素影響第 i 天可以進行的操作,進而影響最大收益。
因此對 T[i][k]
的定義需要分成兩項:
-
T[i][k][0]
表示在第 i 天結束時,最多進行 k 次交易且在進行操作後持有 0 份股票的情況下可以獲得的最大收益; -
T[i][k][1]
表示在第 i 天結束時,最多進行 k 次交易且在進行操作後持有 1 份股票的情況下可以獲得的最大收益。使用新的狀態表示之後,可以得到基準情況和狀態轉移方程。
基準情況:
T[-1][k][0] = 0, T[-1][k][1] = -Infinity
T[i][0][0] = 0, T[i][0][1] = -Infinity
狀態轉移方程:
T[i][k][0] = max(T[i - 1][k][0], T[i - 1][k][1] + prices[i])
T[i][k][1] = max(T[i - 1][k][1], T[i - 1][k - 1][0] - prices[i])
基準情況中,T[-1][k][0] = T[i][0][0] = 0
的含義和上文相同,T[-1][k][1] = T[i][0][1] = -Infinity
的含義是在沒有進行股票交易時不允許持有股票。
對於狀態轉移方程中的 T[i][k][0]
,第 i 天進行的操作只能是休息或賣出,因爲在第 i 天結束時持有的股票數量是 0。T[i - 1][k][0]
是休息操作可以得到的最大收益,T[i - 1][k][1] + prices[i]
是賣出操作可以得到的最大收益。
注意到允許的最大交易次數是不變的,因爲每次交易包含兩次成對的操作,買入和賣出。
只有買入操作會改變允許的最大交易次數。
對於狀態轉移方程中的 T[i][k][1]
,第 i 天進行的操作只能是休息或買入,因爲在第 i 天結束時持有的股票數量是 1。T[i - 1][k][1]
是休息操作可以得到的最大收益,T[i - 1][k - 1][0] - prices[i]
是買入操作可以得到的最大收益。
注意到允許的最大交易次數減少了一次,因爲每次買入操作會使用一次交易。
爲了得到最後一天結束時的最大收益,可以遍歷股票價格數組,根據狀態轉移方程計算 T[i][k][0]
和 T[i][k][1]
的值。最終答案是 T[n - 1][k][0]
,因爲結束時持有 0 份股票的收益一定大於持有 1 份股票的收益。
二、應用於特殊情況
上述六個股票問題是根據 k 的值進行分類的,其中 k 是允許的最大交易次數。最後兩個問題有附加限制,包括「冷凍期」和「手續費」。通解可以應用於每個股票問題。
情況一:k = 1
情況一對應的題目是「121. 買賣股票的最佳時機」。
對於情況一,每天有兩個未知變量:T[i][1][0]
和 T[i][1][1]
,狀態轉移方程如下:
T[i][1][0] = max(T[i - 1][1][0], T[i - 1][1][1] + prices[i])
T[i][1][1] = max(T[i - 1][1][1], T[i - 1][0][0] - prices[i]) = max(T[i - 1][1][1], -prices[i])
第二個狀態轉移方程利用了 T[i][0][0] = 0
。
根據上述狀態轉移方程,可以寫出時間複雜度爲 O(n) 和空間複雜度爲 O(n) 的解法。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
int[][] dp = new int[length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], -prices[i]);
}
return dp[length - 1][0];
}
}
如果注意到第 i 天的最大收益只和第 i - 1 天的最大收益相關,空間複雜度可以降到 O(1)。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int profit0 = 0, profit1 = -prices[0];
int length = prices.length;
for (int i = 1; i < length; i++) {
profit0 = Math.max(profit0, profit1 + prices[i]);
profit1 = Math.max(profit1, -prices[i]);
}
return profit0;
}
}
現在對上述解法進行分析。對於循環中的部分,profit1 實際上只是表示到第 i 天的股票價格的相反數中的最大值,或者等價地表示到第 i 天的股票價格的最小值。對於 profit0,只需要決定賣出和休息中的哪項操作可以得到更高的收益。如果進行賣出操作,則買入股票的價格爲 profit1,即第 i 天之前(不含第 i 天)的最低股票價格。
這正是現實中爲了獲得最大收益會做的事情。但是這種做法不是唯一適用於這種情況的解決方案。
情況二:k 爲正無窮
情況二對應的題目是「122. 買賣股票的最佳時機 II」。
如果 k 爲正無窮,則 k 和 k - 1 可以看成是相同的,因此有 T[i - 1][k - 1][0] = T[i - 1][k][0]
和 T[i - 1][k - 1][1] = T[i - 1][k][1]
。每天仍有兩個未知變量:T[i][k][0] 和 T[i][k][1]
,其中 k 爲正無窮,狀態轉移方程如下:
T[i][k][0] = max(T[i - 1][k][0], T[i - 1][k][1] + prices[i])
T[i][k][1] = max(T[i - 1][k][1], T[i - 1][k - 1][0] - prices[i]) = max(T[i - 1][k][1], T[i - 1][k][0] - prices[i])
第二個狀態轉移方程利用了 T[i - 1][k - 1][0] = T[i - 1][k][0]
。
根據上述狀態轉移方程,可以寫出時間複雜度爲 O(n) 和空間複雜度爲 O(n) 的解法。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
int[][] dp = new int[length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[length - 1][0];
}
}
如果注意到第 i 天的最大收益只和第 i - 1 天的最大收益相關,空間複雜度可以降到 O(1)。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int profit0 = 0, profit1 = -prices[0];
int length = prices.length;
for (int i = 1; i < length; i++) {
int newProfit0 = Math.max(profit0, profit1 + prices[i]);
int newProfit1 = Math.max(profit1, profit0 - prices[i]);
profit0 = newProfit0;
profit1 = newProfit1;
}
return profit0;
}
}
這個解法提供了獲得最大收益的貪心策略:可能的情況下,在每個局部最小值買入股票,然後在之後遇到的第一個局部最大值賣出股票。這個做法等價於找到股票價格數組中的遞增子數組,對於每個遞增子數組,在開始位置買入並在結束位置賣出。
可以看到,這和累計收益是相同的,只要這樣的操作的收益爲正。
情況三:k = 2
情況三對應的題目是「123. 買賣股票的最佳時機 III」。
情況三和情況一相似,區別之處是,對於情況三,每天有四個未知變量:T[i][1][0]
、T[i][1][1]
、T[i][2][0]
、T[i][2][1]
,狀態轉移方程如下:
T[i][2][0] = max(T[i - 1][2][0], T[i - 1][2][1] + prices[i])
T[i][2][1] = max(T[i - 1][2][1], T[i - 1][1][0] - prices[i])
T[i][1][0] = max(T[i - 1][1][0], T[i - 1][1][1] + prices[i])
T[i][1][1] = max(T[i - 1][1][1], T[i - 1][0][0] - prices[i]) = max(T[i - 1][1][1], -prices[i])
第四個狀態轉移方程利用了 T[i][0][0] = 0
。
根據上述狀態轉移方程,可以寫出時間複雜度爲 O(n) 和空間複雜度爲 O(n) 的解法。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
int[][][] dp = new int[length][3][2];
dp[0][1][0] = 0;
dp[0][1][1] = -prices[0];
dp[0][2][0] = 0;
dp[0][2][1] = -prices[0];
for (int i = 1; i < length; i++) {
dp[i][2][0] = Math.max(dp[i - 1][2][0], dp[i - 1][2][1] + prices[i]);
dp[i][2][1] = Math.max(dp[i - 1][2][1], dp[i - 1][1][0] - prices[i]);
dp[i][1][0] = Math.max(dp[i - 1][1][0], dp[i - 1][1][1] + prices[i]);
dp[i][1][1] = Math.max(dp[i - 1][1][1], dp[i - 1][0][0] - prices[i]);
}
return dp[length - 1][2][0];
}
}
如果注意到第 i 天的最大收益只和第 i - 1 天的最大收益相關,空間複雜度可以降到 O(1)。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int profitOne0 = 0, profitOne1 = -prices[0], profitTwo0 = 0, profitTwo1 = -prices[0];
int length = prices.length;
for (int i = 1; i < length; i++) {
profitTwo0 = Math.max(profitTwo0, profitTwo1 + prices[i]);
profitTwo1 = Math.max(profitTwo1, profitOne0 - prices[i]);
profitOne0 = Math.max(profitOne0, profitOne1 + prices[i]);
profitOne1 = Math.max(profitOne1, -prices[i]);
}
return profitTwo0;
}
}
情況四:k 爲任意值
情況四對應的題目是「188. 買賣股票的最佳時機 IV」。
情況四是最通用的情況,對於每一天需要使用不同的 k 值更新所有的最大收益,對應持有 0 份股票或 1 份股票。如果 k 超過一個臨界值,最大收益就不再取決於允許的最大交易次數,而是取決於股票價格數組的長度,因此可以進行優化。
一個有收益的交易至少需要兩天(在前一天買入,在後一天賣出,前提是買入價格低於賣出價格)。如果股票價格數組的長度爲 n,則有收益的交易的數量最多爲 n / 2(整數除法)。因此 k 的臨界值是 n / 2。如果給定的 k 不小於臨界值,即 k >= n / 2,則可以將 k 擴展爲正無窮,此時問題等價於情況二。
根據狀態轉移方程,可以寫出時間複雜度爲 O(nk) 和空間複雜度爲 O(nk) 的解法。
class Solution {
public int maxProfit(int k, int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
if (k >= length / 2) {
return maxProfit(prices);
}
int[][][] dp = new int[length][k + 1][2];
for (int i = 1; i <= k; i++) {
dp[0][i][0] = 0;
dp[0][i][1] = -prices[0];
}
for (int i = 1; i < length; i++) {
for (int j = k; j > 0; j--) {
dp[i][j][0] = Math.max(dp[i - 1][j][0], dp[i - 1][j][1] + prices[i]);
dp[i][j][1] = Math.max(dp[i - 1][j][1], dp[i - 1][j - 1][0] - prices[i]);
}
}
return dp[length - 1][k][0];
}
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
int[][] dp = new int[length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i]);
}
return dp[length - 1][0];
}
}
如果注意到第 i 天的最大收益只和第 i - 1 天的最大收益相關,空間複雜度可以降到 O(k)。
class Solution {
public int maxProfit(int k, int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
if (k >= length / 2) {
return maxProfit(prices);
}
int[][] dp = new int[k + 1][2];
for (int i = 1; i <= k; i++) {
dp[i][0] = 0;
dp[i][1] = -prices[0];
}
for (int i = 1; i < length; i++) {
for (int j = k; j > 0; j--) {
dp[j][0] = Math.max(dp[j][0], dp[j][1] + prices[i]);
dp[j][1] = Math.max(dp[j][1], dp[j - 1][0] - prices[i]);
}
}
return dp[k][0];
}
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int profit0 = 0, profit1 = -prices[0];
int length = prices.length;
for (int i = 1; i < length; i++) {
int newProfit0 = Math.max(profit0, profit1 + prices[i]);
int newProfit1 = Math.max(profit1, profit0 - prices[i]);
profit0 = newProfit0;
profit1 = newProfit1;
}
return profit0;
}
}
如果不根據 k 的值進行優化,在 k 的值很大的時候會超出時間限制。
情況五:k 爲正無窮但有冷卻時間
情況五對應的題目是「309. 最佳買賣股票時機含冷凍期」。
由於具有相同的 k 值,因此情況五和情況二非常相似,不同之處在於情況五有「冷卻時間」的限制,因此需要對狀態轉移方程進行一些修改。
情況二的狀態轉移方程如下:
T[i][k][0] = max(T[i - 1][k][0], T[i - 1][k][1] + prices[i])
T[i][k][1] = max(T[i - 1][k][1], T[i - 1][k][0] - prices[i])
但是在有「冷卻時間」的情況下,如果在第 i - 1 天賣出了股票,就不能在第 i 天買入股票。因此,如果要在第 i 天買入股票,第二個狀態轉移方程中就不能使用 T[i - 1][k][0]
,而應該使用 T[i - 2][k][0]
。
狀態轉移方程中的別的項保持不變,新的狀態轉移方程如下:
T[i][k][0] = max(T[i - 1][k][0], T[i - 1][k][1] + prices[i])
T[i][k][1] = max(T[i - 1][k][1], T[i - 2][k][0] - prices[i])
根據上述狀態轉移方程,可以寫出時間複雜度爲 O(n)和空間複雜度爲 O(n) 的解法。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
int[][] dp = new int[length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0];
for (int i = 1; i < length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], (i >= 2 ? dp[i - 2][0] : 0) - prices[i]);
}
return dp[length - 1][0];
}
}
如果注意到第 i 天的最大收益只和第 i - 1 天和第 i - 2 天的最大收益相關,空間複雜度可以降到 O(1)。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int prevProfit0 = 0, profit0 = 0, profit1 = -prices[0];
int length = prices.length;
for (int i = 1; i < length; i++) {
int nextProfit0 = Math.max(profit0, profit1 + prices[i]);
int nextProfit1 = Math.max(profit1, prevProfit0 - prices[i]);
prevProfit0 = profit0;
profit0 = nextProfit0;
profit1 = nextProfit1;
}
return profit0;
}
}
情況六:k 爲正無窮但有手續費
情況六對應的題目是「714. 買賣股票的最佳時機含手續費」。
由於具有相同的 k 值,因此情況六和情況二非常相似,不同之處在於情況六有「手續費」,因此需要對狀態轉移方程進行一些修改。
情況二的狀態轉移方程如下:
T[i][k][0] = max(T[i - 1][k][0], T[i - 1][k][1] + prices[i])
T[i][k][1] = max(T[i - 1][k][1], T[i - 1][k][0] - prices[i])
由於需要對每次交易付手續費,因此在每次買入或賣出股票之後的收益需要扣除手續費,新的狀態轉移方程有兩種表示方法。
第一種表示方法,在每次買入股票時扣除手續費:
T[i][k][0] = max(T[i - 1][k][0], T[i - 1][k][1] + prices[i])
T[i][k][1] = max(T[i - 1][k][1], T[i - 1][k][0] - prices[i] - fee)
第二種表示方法,在每次賣出股票時扣除手續費:
T[i][k][0] = max(T[i - 1][k][0], T[i - 1][k][1] + prices[i] - fee)
T[i][k][1] = max(T[i - 1][k][1], T[i - 1][k][0] - prices[i])
根據上述狀態轉移方程,可以寫出時間複雜度爲 O(n)和空間複雜度爲 O(n) 的解法。
class Solution {
public int maxProfit(int[] prices, int fee) {
if (prices == null || prices.length == 0) {
return 0;
}
int length = prices.length;
int[][] dp = new int[length][2];
dp[0][0] = 0;
dp[0][1] = -prices[0] - fee;
for (int i = 1; i < length; i++) {
dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1] + prices[i]);
dp[i][1] = Math.max(dp[i - 1][1], dp[i - 1][0] - prices[i] - fee);
}
return dp[length - 1][0];
}
}
如果注意到第 i 天的最大收益只和第 i - 1 天的最大收益相關,空間複雜度可以降到 O(1)。
class Solution {
public int maxProfit(int[] prices) {
if (prices == null || prices.length == 0) {
return 0;
}
int profit0 = 0, profit1 = -prices[0] - fee;
int length = prices.length;
for (int i = 1; i < length; i++) {
int newProfit0 = Math.max(profit0, profit1 + prices[i]);
int newProfit1 = Math.max(profit1, profit0 - prices[i] - fee);
profit0 = newProfit0;
profit1 = newProfit1;
}
return profit0;
}
}
三、總結
總而言之,股票問題最通用的情況由三個特徵決定:當前的天數 i、允許的最大交易次數 k 以及每天結束時持有的股票數。
這篇文章闡述了最大利潤的狀態轉移方程和終止條件,由此可以得到時間複雜度爲 O(nk) 和空間複雜度爲 O(k) 的解法。
該解法可以應用於六個問題,對於最後兩個問題,需要將狀態轉移方程進行一些修改。
本文分享自微信公衆號 - 五分鐘學算法(CXYxiaowu)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。