leetcode 121&122 买卖股票的最佳时机-python 暴力法+一次遍历+动态规划


提示:LeetCode121 ,买卖股票的最佳时机(简单):
提示:LeetCode122 ,买卖股票的最佳时机(中等):

一、题目LeetCode121

买卖股票的最佳时机(简单):

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

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

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

示例 1:

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

解题思路

方法1.暴力法

示例 1:
输入:[7,1,5,3,6,4]
输出:5

问题:第x天买入股票,第y天卖出股票时,能够获得最大利润
条件:1. y>x 2.只能交易一次
分析过程:
1.第1天作为买入价格prices[0],分别计算第2天到第y天的价格j,相减求出本次循环最大值。
2.第2天作为买入价格prices[1],同理
3.更新最大值

代码如下:


# 方法一: 暴力求解(会超出时间限制)
# class Solution:
#     def maxProfit(self, prices: List[int]) -> int:
#         if prices is None:
#             return 0
#         total_day=len(prices)
#         if total_day==1:
#             return 0
#         bonus=0
#         result=[]
#         for i in range(1,total_day):   # 买 从1天开始
#             for j in range(i+1,total_day+1):  # 卖 从第2天
#                 diff=prices[j-1]-prices[i-1]   
#                 if diff >0:
#                     bonus=max(bonus,diff)
#             result.append(bonus)

#         if result is None:
#             return 0
#         else:
#             return max(result)

方法2.一次遍历

想要求最大利润 需要在最低点买入 ,记录最低点价格 然后遍历每一天的价格 与最低相减

代码如下:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        # inf = int(1e9)  # 1e9 表示一个大数,记为无穷大
        # minprice = inf
        minprice=float('INF')
        max_bonus=0
        for price in prices:
            max_bonus=max(max_bonus,price-minprice)
            minprice=min(minprice,price)
        return max_bonus

方法3.dp数组迭代求解

示例分析图

代码如下:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        res=float('INF')
        dp=[res]*len(prices)   #  dp[i]  第i天卖出时存放的最大利润
        # dp_min=prices[:]    # 存放最低购入价
        dp_min=[0]*len(prices)
        dp[0]=0
        dp_min[0]=prices[0]   # 初始化 第一天的价格即为最低价

        for i in range(1,len(prices)):
            dp[i]=prices[i]-dp_min[i-1]
            dp_min[i]=min(prices[i],dp_min[i-1])
        return max(dp)

二、题目LeetCode122

买卖股票的最佳时机(中等):
给你一个整数数组 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 。

解题思路

方法3.dp数组迭代求解

示例分析图

代码如下:

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        total_len=len(prices)
        # dp[i][0]  dp二维数组用于存放最大利润    [i][0]表示第i天结束时,手上没有股票时的最大利润
        # dp[i][1]  dp二维数组用于存放最大利润    [i][1]表示第i天结束时,手上拥有股票时的最大利润
        dp=[[0]*2 for _ in range(total_len)]     #2列total_len行 
        dp[0][0]=0
        dp[0][1]=-prices[0]

        for i in range(1,total_len):
            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[total_len-1][0]        # 返回最大利润 此时手上没有股票



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值