买卖股票的最佳时机

买卖股票的最佳时机 II

给定一个数组 prices ,其中 prices[i] 是一支给定股票第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

  • 动态规划:
    一维:交易日期,价格数组长度
    二维:两种状态:持有或现金
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        dp = [[0]*2 for _ in range(len(prices))]
        dp[0][1] = -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][0]
  • 空间优化:
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        cash, hold = 0, -prices[0]
        for i in range(1, len(prices)):
            cash, hold = max(cash, hold + prices[i]), max(hold, cash - prices[i])
        return cash
  • 贪心:
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        max_pro = 0
        for i in range(1, len(prices)):
            diff = prices[i] - prices[i - 1]
            if diff > 0:
                max_pro += diff
        return max_pro

买卖股票的最佳时机 III

给定一个数组,它的第 i 个元素是一支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 两笔 交易。

注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。

  • 动态规划:
    • 一维:交易日期,价格数组长度
    • 二维:是否持有(持有/未持有)
    • 三维:交易次数(0次1次2次)
class Solution:
    def maxProfit(self, prices):
        if prices==[]:
            return 0
        length=len(prices)
        #结束时的最高利润=[天数][是否持有股票][卖出次数]
        dp=[ [[0,0,0],[0,0,0] ] for i in range(0,length) ]
        #第一天休息
        dp[0][0][0]=0
        #第一天买入
        dp[0][1][0]=-prices[0]
        # 第一天不可能已经有卖出
        dp[0][0][1] = float('-inf')
        dp[0][0][2] = float('-inf')
        #第一天不可能已经卖出
        dp[0][1][1]=float('-inf')
        dp[0][1][2]=float('-inf')
        for i in range(1,length):
            #未持股,未卖出过,说明从未进行过买卖
            dp[i][0][0]=0
            #未持股,卖出过1次,可能是今天卖的,可能是之前卖的
            dp[i][0][1]=max(dp[i-1][1][0]+prices[i],dp[i-1][0][1])
            #未持股,卖出过2次,可能是今天卖的,可能是之前卖的
            dp[i][0][2]=max(dp[i-1][1][1]+prices[i],dp[i-1][0][2])
            #持股,未卖出过,可能是今天买的,可能是之前买的
            dp[i][1][0]=max(dp[i-1][0][0]-prices[i],dp[i-1][1][0])
            #持股,卖出过1次,可能是今天买的,可能是之前买的
            dp[i][1][1]=max(dp[i-1][0][1]-prices[i],dp[i-1][1][1])
            #持股,卖出过2次,不可能
            dp[i][1][2]=float('-inf')
        return max(dp[length-1][0][1],dp[length-1][0][2],0)

滚动数组优化空间:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        cash0, cash1, cash2, hold0, hold1 = 0, 0, 0, -prices[0], -prices[0]
        for i in range(1, len(prices)):
            cash0, cash1, cash2, hold0, hold1 = 0, max(cash1, hold0 + prices[i]), max(cash2, hold1 + prices[i]),max(hold0, cash0 - prices[i]), max(hold1, cash1 - prices[i])
                                                    
        return max(cash2,cash1)

309. 最佳买卖股票时机含冷冻期

给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 。​

设计一个算法计算出最大利润。在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股票):

你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。
卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。

思路:

  • 动态规划:
    • 一维:交易日期
    • 二维:三种状态 现金-持有-冷冻
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        cash, fre, hold = 0, 0, -prices[0]
        for i in range(1,len(prices)):
            cash, fre, hold = max(cash, fre), hold + prices[i], max(hold, cash - prices[i])
        return max(cash, fre)

714. 买卖股票的最佳时机含手续费

给定一个整数数组 prices,其中第 i 个元素代表了第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。

你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。

返回获得利润的最大值。

注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。

思路: 与II相同,只是买或者卖多加一个fee

传统dp:

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        n  = len(prices)
        dp = [[0]*2 for _ in range(n)]
        dp[0][1] = -prices[0] - fee
        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][0]

优化空间:

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        n  = len(prices)
        cash = 0
        hold = -prices[0] - fee
        for i in range(1, n):
            cash, hold = max(cash, hold + prices[i]), max(hold, cash - prices[i] - fee)
        return cash

贪心:
思路: II中是将所有的增长区间,即差为正的区间相加,这里多了交易费用,同样的,我们将高于成本+fee的视为正数,因为只用一次费用,所以将更新后的cost置为 当前价格-fee

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        max_pro = 0
        min_cost = prices[0]
        for i in range(1, len(prices)):
            if min_cost > prices[i]:
                min_cost = prices[i]
            elif prices[i] > min_cost + fee:
                max_pro += prices[i] - min_cost - fee
                min_cost = prices[i] - fee
        return max_pro 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值