题解 | 买卖股票的最好时机(一)

买卖股票的最好时机(一)

https://www.nowcoder.com/practice/64b4262d4e6d4f6181cd45446a5821ec

#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 
# @param prices int整型一维数组 
# @return int整型
#
class Solution:
    def maxProfit(self , prices: List[int]) -> int:
        # write code here
        n = len(prices)
        if n==0 or n==1:
            return 0
        minprice, maxprofit = prices[0], 0#最低价格,最大利润
        for x in prices:#遍历列表
            maxprofit = max(maxprofit,x-minprice)#寻找最大利润
            minprice = min(minprice,x)#寻找最低价格
        return maxprofit

全部评论

相关推荐

12-07 10:09
复旦大学 Java
点赞 评论 收藏
分享
程序员牛肉:你这其实一点都没包装,标准的流水线产品。 实习现在不一定能解决你的问题,你太浮躁了。你看了多少源码?看了多少技术博客?真的没必要这么浮躁的着急找实习,沉下心来学习
投递实习岗位前的准备
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务