Python Leetcode买卖股票

所有股票问题基本都可以使用动态规划进行求解。
本文所使用的动态规划方法均参考一下链接:
买卖股票专题讲解

买卖股票之只能一次交易

在这里插入图片描述

# # 一次遍历
# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         max_p = 0
#         min_p = prices[0]
#         for i in range(1, len(prices)):
#             min_p = min(min_p, prices[i])
#             max_p = max(prices[i] - min_p, max_p)
#         return max_p

# #一维动态规划
# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         dp = [0 for _ in range(len(prices))]
#         min_p = prices[0]
#         for i in range(1, len(prices)):
#             min_p = min(min_p, prices[i])
#             dp[i] = max(dp[i - 1], prices[i] - min_p)
#         return dp[-1]

# #二维动态规划
# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         dp = [[0] * 2 for _ in range(len(prices))]
#         dp[0][0] = -prices[0]
#         for i in range(1, len(prices)):
#             dp[i][0] = max(dp[i - 1][0], - prices[i])
#             dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])
#         return dp[-1][1]

# 单调栈
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        stack = [prices[0]]
        ans = 0
        for i in range(1, len(prices)):
            if prices[i] > stack[-1]:
                ans = max(prices[i] - stack[-1], ans)
            else:
                stack.append(prices[i])
        return ans

买卖股票之可多次交易

在这里插入图片描述

# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         stack = [prices[0]]
#         ans = 0
#         for i in range(1, len(prices)):
#             if stack[-1] <= prices[i]:
#                 stack.append(prices[i])
#             else:
#                 ans += stack[-1] - stack[0]
#                 stack = [prices[i]]
#         return ans + stack[-1] - stack[0]

# # 动态规划
# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         dp = [[0] * 2 for _ in range(len(prices))]
#         dp[0][0] = -prices[0]
#         for i in range(1, len(prices)):
#             dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i])
#             dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])
#         return dp[-1][1]

# 贪心
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        ans = 0
        for i in range(1, len(prices)):
            ans += max(prices[i] - prices[i - 1], 0)
        return ans

注意:
在上一题的只能进行一次股票买卖题中,因为股票全程只能买卖一次,所以如果买入股票,那么第i天持有股票即dp[i][0]一定就是 -prices[i]。而本题,因为一只股票可以买卖多次,所以当第i天买入股票的时候,所持有的现金可能有之前买卖过的利润。

dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i]) # 可进行多次买卖的代码
dp[i][0] = max(dp[i - 1][0], - prices[i])  # 只能进行一次买卖的代码

买卖股票之规定买卖次数

在这里插入图片描述

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        dp = [[0] * 4 for _ in range(len(prices))]
        dp[0][0] = dp[0][2] = -prices[0]
        for i in range(1, len(prices)):
            dp[i][0] = max(dp[i - 1][0], -prices[i])
            dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])
            dp[i][2] = max(dp[i - 1][2], dp[i - 1][1] - prices[i])
            dp[i][3] = max(dp[i - 1][3], dp[i - 1][2] + prices[i])
        return dp[-1][-1]
# 压缩
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        n = len(prices)
        dp = [0 for _ in range(4)]
        dp[0] = dp[2] = -prices[0]
        for i in range(1, n):
            dp[0] = max(dp[0], -prices[i])
            dp[1] = max(dp[1], dp[0] + prices[i])
            dp[2] = max(dp[2], dp[1] - prices[i])
            dp[3] = max(dp[3], dp[2] + prices[i])
        return dp[3]

买卖股票之可变的买卖次数

在这里插入图片描述

# 二维数组DP
class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        if not prices:
            return 0
        n = len(prices)
        dp = [[0] * (2 * k + 1) for _ in range(n)]
        for i in range(1, 2*k + 1, 2):
            dp[0][i] = -prices[0]
        for i in range(1, n):
            for j in range(0, 2*k - 1, 2):
                dp[i][j+1] = max(dp[i - 1][j + 1], dp[i - 1][j] - prices[i])
                dp[i][j + 2] = max(dp[i - 1][j + 2], dp[i - 1][j + 1] + prices[i])
        return dp[-1][-1]
# 压缩
class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        if not prices:
            return 0
        n = len(prices)
        dp = [0] * (2 * k + 1) # 奇数时买入,偶数时卖出(除0外,0时表示没有进行任何操作)
        for i in range(1, 2*k + 1, 2):
            dp[i] = -prices[0]
        for i in range(1, n):
            for j in range(0, 2*k - 1, 2):
                dp[j + 1] = max(dp[j + 1], dp[j] - prices[i])
                dp[j + 2] = max(dp[j + 2], dp[j + 1] + prices[i])
        return dp[-1]

# 创建buy和sell两个二维数组
class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        if not prices:
            return 0
        n = len(prices)
        k = min(k, n // 2) # 剪枝,买卖的最大次数为prices的长度值的一半
        buy = [[0] * (k + 1) for _ in range(n)] # 由于k可能为1,即进行一次买卖,未来避免没有进行下面的嵌套循环,因此需要将k加一,此时下标0可以表示不进行操作时的值,下标k即表示第k(k>0)次买/卖后的值
        sell = [[0] * (k + 1) for _ in range(n)] #这两行的赋值操作不要写成一行buy = sell = [[0] * (k + 1) for _ in range(n)],否则答案错误!
        for i in range(k + 1):
            buy[0][i] = -prices[0]
        for i in range(1, n):
            for j in range(1, k + 1):
                buy[i][j] = max(buy[i - 1][j], sell[i - 1][j - 1] - prices[i])
                sell[i][j] = max(sell[i - 1][j], buy[i - 1][j] + prices[i])
        return max(sell[n - 1])
# 压缩
class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        if not prices:
            return 0
        n = len(prices)
        k = min(k, n // 2)
        buy = [0] * (k + 1) 
        sell = [0] * (k + 1) #这两行的赋值操作也不能写成一行,原理同上!
        for i in range(1, k + 1):  #为了便于理解,此处可以从1开始索引
            buy[i] = -prices[0]
        for i in range(1, n):
            for j in range(1, k + 1):
                buy[j] = max(buy[j], sell[j - 1] - prices[i])
                sell[j] = max(sell[j], buy[j] + prices[i])
        return sell[-1]

注意:

  • 二维列表的创建时,不能进行如下操作:dp = [[0] * n] *m,该结果与dp = [[0] * n for _ in range(m)]所产生的结果会不一样,因为[0] * n操作产生了一个列表,此时再去进行* m操作,是对所产生的列表进行地址复制,最后得到的二维列表的每一行实际都是第一行的列表值,改变其他行的列表值便会改变第一行的数值。
  • 多个多维列表赋值时,也不能直接进行一次性赋值操作,即不能buy = sell = [[0] * n for _ in range(m)],而应该分开赋值,原因同上面的一样,也是由于赋值的是地址。但单个的数值可以进行一次性赋值操作,即a = b = [0]
  • 该题由于k不确定,可能为1,为了避免没有进行嵌套循环而得到结果0,我们可以多设置一个‘没有做任何操作(0表示)’,因此在创建列表时,状态个数为2 * k + 1,具体可以参照188股票买卖题解

买卖股票之含冷冻期

在这里插入图片描述

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices: return 0
        n = len(prices)
        dp = [[0] * 3 for _ in range(n)]
        dp[0][0] = -prices[0]
        # dp[i][0]:手上持有股票的最大收益
        # dp[i][1]:手上不持有股票,并且处于冷冻期中的累计最大收益
        # dp[i][2]:手上不持有股票,并且不在冷冻期中的累计最大收益
        for i in range(1, n):
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][2] - prices[i])
            dp[i][1] = dp[i - 1][0] + prices[i]
            dp[i][2] = max(dp[i - 1][1], dp[i - 1][2])
        return max(dp[-1][1], dp[-1][2])


class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices: return 0
        n = len(prices)
        dp = [[0] * 3 for _ in range(n)]
        dp[0][0] = -prices[0]
        # dp[i][0]:买入
        # dp[i][1]:卖出(可能含冷冻期)
        # dp[i][2]:冷冻期
        for i in range(1, n):
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][2] - prices[i])
            dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i])
            dp[i][2] = dp[i - 1][1]
        return dp[-1][1]

买卖股票之含手续费

在这里插入图片描述

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        if not prices: return 0
        n = len(prices)
        dp = [[0] * 2 for _ in range(n)]
        dp[0][0] = -prices[0]
        for i in range(1, n):
            dp[i][0] = max(dp[i - 1][0], dp[i - 1][1] - prices[i])
            dp[i][1] = max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee)
        return dp[-1][1]

# 压缩
class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        if not prices: return 0
        n = len(prices)
        dp = [-prices[0], 0]  
        for i in range(1, n):
            dp[0] = max(dp[0], dp[1] - prices[i])
            dp[1] = max(dp[1], dp[0] + prices[i] - fee)
        return dp[1]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值