Skip to content

122. 买卖股票的最佳时机 II

题目

给你一个整数数组 prices ,其中 prices[i] 表示某支股票第 i 天的价格。

在每一天,你可以决定是否购买和/或出售股票。你在任何时候 最多 只能持有 一股 股票。你也可以先购买,然后在 同一天 出售。

返回 你能获得的 最大 利润

示例 1:

输入:prices = [7,1,5,3,6,4]
输出:7
解释:在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
     随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6 - 3 = 3 。
     总利润为 4 + 3 = 7 。

示例 2:

输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5 - 1 = 4 。
     总利润为 4 。

示例 3:

输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 交易无法获得正利润,所以不参与交易可以获得最大利润,最大利润为 0 。

提示:

  • 1 <= prices.length <= 3 * 10^4
  • 0 <= prices[i] <= 10^4

解答

思路:状态机记忆化搜索

买卖股票问题是典型的 状态机 DP 问题,可分为两类:

  • 不限交易次数
  • 至多/恰好/至少交易 k

本题是不限交易次数的类型。为了方便改成递推,我们推荐思考记忆化搜索时采用从最后一天往前思考的方式,我们思考最后一天发生了什么。其中股票的价格如下:

python
prices = [7, 1, 5, 3, 6, 4]

从第 0 天开始到第 5 天结束时的利润 = 从第 0 天开始到第 4 天结束时的利润 + 第 5 天的利润。关键在于到第 5 天时手上是否持有股票。

回溯三问:

  • 当前操作:在第 i 天能做的事情
    • 什么都不做
    • 买入股票
    • 卖出股票
  • 当前问题:
    • 到第 i 天结束时,且 持有股票 时的最大利润
    • 到第 i 天结束时,且 未持有股票 时的最大利润
  • 它的子问题:
    • 到第 i1 天结束时,且 持有股票 时的最大利润
    • 到第 i1 天结束时,且 未持有股票 时的最大利润

状态机图:

image-20240119233419399

定义状态和状态转移方程:

  • dfs(i, 0) 表示到第 i 天结束时,且 未持有股票 时的最大利润
  • dfs(i, 1) 表示到第 i 天结束时,且 持有股票 时的最大利润
  • max(dfs(i, 0), dfs(i, 1)) 表示第 i+1 天开始时的最大利润

状态转移方程:

image-20240119233646172

dfs(i,0)=max(dfs(i1,0),dfs(i1,1)+prices[i])dfs(i,1)=max(dfs(i1,1),dfs(i1,0)prices[i])

递归边界:

  • dfs(-1, 0) = 0 表示第 0 天开始且没有持有股票,利润为 0
  • dfs(-1, 1) = -inf 表示第 0 天开始且持有股票,这是不可能的,因此所有由它转移得到的答案都是错的,于是把它的利润设置为负无穷

递归入口:max(dfs(n - 1, 0), dfs(n - 1, 1)) = dfs(n - 1, 0)

代码

python
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        n = len(prices)

        @cache
        def dfs(i, hold):
            if i < 0:
                return -inf if hold else 0
            
            if hold:
                return max(dfs(i - 1, True), dfs(i - 1, False) - prices[i])
            
            return max(dfs(i - 1, False), dfs(i - 1, True) + prices[i])
        
        return dfs(n - 1, False)

思路:递推

原始递推式:

f[i][0]=max(f[i1][0],f[i1][1]+prices[i])f[i][1]=max(f[i1][1],f[i1][0]prices[i])

防止负下标:

f[i+1][0]=max(f[i][0],f[i][1]+prices[i])f[i+1][1]=max(f[i][1],f[i][0]prices[i])

起始值:

f[0][0]=0,f[0][1]=

答案:

f[n][0]

代码

python
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        n = len(prices)

        f = [[0] * 2 for _ in range(n + 1)]
        f[0][1] = -inf

        for i, p in enumerate(prices):
            f[i + 1][0] = max(f[i][0], f[i][1] + p)
            f[i + 1][1] = max(f[i][1], f[i][0] - p)
        
        return f[n][0]

思路:空间优化

f[i] 只用到了 f[i - 1][0], f[i - 1][1],因此用两个数字滚动计算即可。

代码

python
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        n = len(prices)
        
        f0 = 0
        f1 = -inf

        for p in prices:
            new_f0 = max(f0, f1 + p)
            new_f1 = max(f1, f0 - p)

            f0 = new_f0
            f1 = new_f1
        
        return f0

Released under the MIT License.