LeetCode 309. Best Time to Buy and Sell Stock with Cooldown

本文探讨了一种股票交易策略问题,利用动态规划算法寻找在特定交易规则下获得的最大利润。通过分析每日股票价格,设计算法完成多次买卖交易,同时遵循不可同时进行多笔交易及卖出后需冷却一天的规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 as many transactions as you like (ie, buy one and sell one share of the stock multiple times) with the following restrictions:

  • You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
  • After you sell your stock, you cannot buy stock on next day. (ie, cooldown 1 day)

Example:

Input: [1,2,3,0,2]
Output: 3 
Explanation: transactions = [buy, sell, cooldown, buy, sell]

------------------------------------------------------------------------

这题容易受到没有cooldown那个影响,总想在低点买入,高点卖出,但是卖出cooldown要放在卖那天还是买之后,不知道。。。所以应该可以想到动态规划。。股票交易的特殊思路很多,这里动态规划是一种通用的思路。。。

但是怎么动态规划呢,如果把买、卖、cooldown都作为状态,cooldown的状态可能是买入后cooldown,也可能是卖出后cooldown,跳转还有额外的限制,想起来非常晕。。

比较简化的办法是把买、卖、cooldown都作为动作,只有手里有没有股票才是状态,这就是典型的状态DP,状态DP一定要简化状态和动作。在这题里,第几天、有没有股票都是状态,更复杂一些,推广到另外几道股票题目,交易了几次也是状态。这样不难写出状态转移方程。

             t[i][0] = max(t[i-1][0],t[i-1][1]+prices[i])
             t[i][1] = max(t[i-1][1],t[i-2][0]-prices[i])

注意这道题的状态转移方程只和前两个状态相关,因此DP数据可以拿掉,变成O(1)的空间复杂度,O(n)的时间复杂度。以下是代码:

class Solution:
    # def maxProfit(self, prices) -> int:
    #     l = len(prices)
    #     if (l <= 1):
    #         return 0
    #     t = [[0 for j in range(2)] for i in range(l)]
    #     t[1][0] = max(0, prices[1]-prices[0]) #bug1: forget this line
    #     t[0][1],t[1][1] = -prices[0],max(-prices[0],-prices[1])
    #     for i in range(2,l):
    #         t[i][0] = max(t[i-1][0],t[i-1][1]+prices[i])
    #         t[i][1] = max(t[i-1][1],t[i-2][0]-prices[i])
    #     return t[l-1][0]

    def maxProfit0(self, prices) -> int:
        l = len(prices)
        if (l <= 1):
            return 0
        ti0_pre, ti0 = 0, max(0, prices[1]-prices[0])
        ti1_pre, ti1 = -prices[0],max(-prices[0],-prices[1])

        for i in range(2,l):
            tmp = ti0
            ti0 = max(ti0, ti1+prices[i])
            ti1 = max(ti1, ti0_pre-prices[i])
            ti0_pre = tmp

        return ti0

s = Solution()
print(s.maxProfit([1,2,3,0,2]))

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值