leetcode刷题-动态规划之买卖股票的最佳时机系列

买卖股票的最佳时机系列

121. 买卖股票的最佳时机

题目

给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。

你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。

返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。

注意: 若 s 和 t 中每个字符出现的次数都相同,则称 s 和 t 互为字母异位词。

示例 1:

输入:[7,1,5,3,6,4]
输出:5
解释:在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。
     注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格;同时,你不能在买入前卖出股票。

示例 2:

输入:prices = [7,6,4,3,1]
输出:0
解释:在这种情况下, 没有交易完成, 所以最大利润为 0。

提示:

  • 1 < = p r i c e s . l e n g t h < = 1 0 5 1 <= prices.length <= 10^5 1<=prices.length<=105
  • 0 < = p r i c e s [ i ] < = 1 0 4 0 <= prices[i] <= 10^4 0<=prices[i]<=104

代码

  • 贪心 动态规划
    时间复杂度 O(N)
    空间复杂度 O(1)
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        # 贪心 + 优化minmax if 的速度比min max快很多
        minPrice, profit = inf, 0
        for price in prices:
            if minPrice > price:
                minPrice = price
            if price-minPrice > profit:
                profit = price-minPrice
        return profit

        # n = len(prices)
        # dp = [[0, 0] for _ in range(n)]
        # dp[0] = [-prices[0], 0]
        # for i in range(1, n):
        #     dp[i][0] = max(dp[i-1][0], - prices[i])
        #     dp[i][1] = max(dp[i-1][1], + prices[i] + dp[i-1][0])
        # return dp[-1][-1]
        k = 1
        n = len(prices)
        dp = [[0, 0]*k for _ in range(n)]
        dp[0] = [-prices[0], 0]*k
        for i in range(1, n):
            for j in range(k):
                dp[i][j*2] = max(dp[i-1][j*2], -prices[i] + (dp[i-1][j*2-1] if j!=0 else 0))
                dp[i][j*2+1] = max(dp[i-1][j*2+1], dp[i-1][j*2] + prices[i])
        return dp[-1][-1]

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 < = p r i c e s . l e n g t h < = 3 ∗ 1 0 4 1 <= prices.length <= 3*10^4 1<=prices.length<=3104
  • 0 < = p r i c e s [ i ] < = 1 0 4 0 <= prices[i] <= 10^4 0<=prices[i]<=104

代码

  • 贪心 动态规划
    时间复杂度 O(N)
    空间复杂度 O(1)
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        # 贪心
        # res = 0
        # for i in range(1, len(prices)):
        #     tmp = prices[i] - prices[i-1]
        #     if tmp > 0:
        #         res += tmp
        #     else:
        #         continue
        # return res
        # 
        n = len(prices)
        dp = [[0,0] for _ in range(n)]
        dp[0] = [-prices[0], 0]
        for i in range(1, n):
            dp[i][0] = max(dp[i-1][0], - prices[i] + dp[i-1][1])
            dp[i][1] = max(dp[i-1][1], + prices[i] + dp[i-1][0])
        return dp[-1][-1]

123. 买卖股票的最佳时机 III

题目

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

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

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

示例 1:

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

示例 2:

输入:prices = [1,2,3,4,5]
输出:4
解释:在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。   
     注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。   
     因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。

示例 3:

输入:prices = [7,6,4,3,1] 
输出:0 
解释:在这个情况下, 没有交易完成, 所以最大利润为 0。

示例 4:

输入:prices = [1]
输出:0

提示:

  • 1 < = p r i c e s . l e n g t h < = 1 0 5 1 <= prices.length <= 10^5 1<=prices.length<=105
  • 0 < = p r i c e s [ i ] < = 1 0 5 0 <= prices[i] <= 10^5 0<=prices[i]<=105

代码

  • 贪心 动态规划
    时间复杂度 O(N)
    空间复杂度 O(1)
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        # 第一次买入,第一次卖出,第二次买入,第二次卖出
        # f1, f2, f3, f4 = -prices[0], 0, -prices[0], 0
        # for price in prices[1:]:
        #     f1 = max(f1, -price)
        #     f2 = max(f2, f1 + price)
        #     f3 = max(f3, f2 - price)
        #     f4 = max(f4, f3 + price)
        # return f4
    #     n = len(prices)
    #     dp = [[0,0,0,0] for _ in range(n)]
    #     dp[0]=[-prices[0],0,-prices[0],0]
    #     for i in range(1,n):
    #         dp[i][0] = max(dp[i-1][0], -prices[i])
    #         dp[i][1] = max(dp[i-1][1], + prices[i] + dp[i-1][0])
    #         dp[i][2] = max(dp[i-1][2], - prices[i] + dp[i-1][1])
    #         dp[i][3] = max(dp[i-1][3], + prices[i] + dp[i-1][2])
    #     return dp[-1][-1]
        n = len(prices)
        k = 2
        dp = [[0, 0]*k for _ in range(n)]
        dp[0] = [-prices[0], 0]*k
        for i in range(1, n):
            for j in range(k):
                dp[i][j*2] = max(dp[i-1][j*2], -prices[i] + (dp[i-1][j*2-1] if j!=0 else 0))
                dp[i][j*2+1] = max(dp[i-1][j*2+1], dp[i-1][j*2] + prices[i])
        return dp[-1][-1]

188. 买卖股票的最佳时机 IV

题目

给你一个整数数组 prices 和一个整数 k ,其中 prices[i] 是某支给定的股票在第 i 天的价格。

设计一个算法来计算你所能获取的最大利润。你最多可以完成 k 笔交易。也就是说,你最多可以买 k 次,卖 k 次。

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

示例 1:

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

示例 2:

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

提示:

  • 1 < = k < = 100 1 <= k <= 100 1<=k<=100
  • 1 < = p r i c e s . l e n g t h < = 1000 1 <= prices.length <= 1000 1<=prices.length<=1000
  • 0 < = p r i c e s [ i ] < = 1000 0 <= prices[i] <= 1000 0<=prices[i]<=1000

代码

  • 贪心 动态规划
    时间复杂度 O(nk)
    空间复杂度 O(nk)
class Solution:
    def maxProfit(self, k: int, prices: List[int]) -> int:
        n = len(prices)
        dp = [[0,0] * k for _ in range(n)]
        dp[0] = [-prices[0], 0] * k
        for i in range(1,n):
            for j in range(k):
                dp[i][j*2] = max(dp[i-1][j*2], -prices[i]+(dp[i-1][j*2-1] if j != 0 else 0))
                dp[i][j*2+1] = max(dp[i-1][j*2+1], +prices[i]+dp[i-1][j*2])
        return dp[-1][-1]

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

题目

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

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

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

示例 1:

输入:prices = [1, 3, 2, 8, 4, 9], fee = 2
输出:8
解释:能够达到的最大利润:  
在此处买入 prices[0] = 1
在此处卖出 prices[3] = 8
在此处买入 prices[4] = 4
在此处卖出 prices[5] = 9
总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8

示例 2:

输入:prices = [1,3,7,5,10,3], fee = 3
输出:6

提示:

  • 1 < = p r i c e s . l e n g t h < = 5 ∗ 1 0 4 1 <= prices.length <= 5*10^4 1<=prices.length<=5104
  • 1 < = p r i c e s [ i ] < = 5 ∗ 1 0 4 1 <= prices[i] <= 5*10^4 1<=prices[i]<=5104
  • 0 < = f e e < = 5 ∗ 1 0 4 0 <= fee <= 5*10^4 0<=fee<=5104

代码

  • 贪心 动态规划
    时间复杂度 O(n)
    空间复杂度 O(n)
class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:
        n = len(prices)
        dp = [[0,0] for _ in range(n)]
        dp[0] = [-prices[0], 0]
        for i in range(1,n):
            dp[i][0] = max(dp[i-1][0], - prices[i] + dp[i-1][1])
            dp[i][1] = max(dp[i-1][1], + prices[i] + dp[i-1][0] - fee)
        return dp[-1][-1]

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

题目

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

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

  • 卖出股票后,你无法在第二天买入股票 (即冷冻期为 1 天)。

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

示例 1:

输入: prices = [1,2,3,0,2]
输出: 3 
解释: 对应的交易状态为: [买入, 卖出, 冷冻期, 买入, 卖出]

示例 2:

输入: prices = [1]
输出: 0

提示:

  • 1 < = p r i c e s . l e n g t h < = 5000 1 <= prices.length <= 5000 1<=prices.length<=5000
  • 0 < = p r i c e s [ i ] < = 1000 0 <= prices[i] <= 1000 0<=prices[i]<=1000

代码

  • 贪心 动态规划
    时间复杂度 O(n)
    空间复杂度 O(n)
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        n = len(prices)
        dp = [[0,0] for _ in range(n)]
        dp[0] = [-prices[0], 0]
        for i in range(1, n):
            dp[i][0] = max(dp[i-1][0], - prices[i] + (dp[i-2][1] if i > 1 else 0))
            dp[i][1] = max(dp[i-1][1], + prices[i] + dp[i-1][0])
        return dp[-1][-1]
  • 20
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值