代码随想录Day 42|Leetcode|Python|121. 买卖股票的最佳时机 ● 122.买卖股票的最佳时机II

121. 买卖股票的最佳时机

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

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

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

解题思路:

贪心算法:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if len(prices)<=1:
            return 0
        #greedy
        min_prices = float('inf')
        max_profit = 0
        for i in range(len(prices)):
            min_prices = min(min_prices, prices[i])
            max_profit = max(max_profit, prices[i]-min_prices)
        return max_profit

确认dp数组含义:dp[i][0]第i天时持有股票时所得最多现金,dp[i][1]第i天时未持有股票时所得最多现金

确认递推公式:dp[i][0] = max(dp[i-1][0], -prices[i]), -prices[i]指第i天买入股票后所持有现金

dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]), dp[i-1][0]+prices[i]指今天卖掉股票

初始化:dp[0][0] = -prices[0], dp[0][1] = 0第一天买入股票和第一天不买股票

遍历顺序:从前到后遍历

打印dp数组

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

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

解题思路:

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

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

返回 你能获得的 最大 利润 。

解题思路:

贪心算法

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        #greedy
        result = 0
        for i in range(1, len(prices)):
            if prices[i] - prices[i-1]>0:
                result += prices[i] - prices[i-1]
        return result

动态规划:

与第一题类似,不同之处在于递推公式要考虑前一天的盈利情况

确认dp数组含义:dp[i][0]第i天时持有股票时所得最多现金,dp[i][1]第i天时未持有股票时所得最多现金

确认递推公式:dp[i][0] = max(dp[i-1][0], dp[i-1][1]-prices[i]),dp[i-1][1]-prices[i]指第i-1天没有股票,但是第i天买入股票,在前一天财产基础dp[i-1][1]上减去当天股票价格,得到目前现金总额

dp[i][1] = max(dp[i-1][1], dp[i-1][0]+prices[i]),dp[i-1][0]+prices[i] 指i-1天持有股票时的金额数加上第i天卖掉股票后赚得现金总额。

初始化:dp[0][0] = -prices[0], dp[0][1] = 0第一天买入股票和第一天不买股票

遍历顺序:从前向后遍历

打印dp数组:[[-7, 0], [-1, 0], [-1, 4], [1, 4], [1, 7], [3, 7]]

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

相关推荐

  1. 代码随想 123. 买卖股票最佳时机 III

    2024-05-09 22:58:04       55 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-09 22:58:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-09 22:58:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-05-09 22:58:04       87 阅读
  4. Python语言-面向对象

    2024-05-09 22:58:04       96 阅读

热门阅读

  1. Mybatis Plus ActiveRecord 模式

    2024-05-09 22:58:04       28 阅读
  2. Linux-笔记 Makefile简单入门

    2024-05-09 22:58:04       29 阅读
  3. 双指针 Leetcode 151 反转字符串中的单词

    2024-05-09 22:58:04       33 阅读
  4. Softmax和Sigmoid

    2024-05-09 22:58:04       34 阅读
  5. deepstream std mean 对应的计算方法

    2024-05-09 22:58:04       28 阅读
  6. 单例模式析构时持久化

    2024-05-09 22:58:04       31 阅读
  7. 蓝桥杯备战3.日期识别——map

    2024-05-09 22:58:04       30 阅读