• 121. Best Time to Buy and Sell Stock


    题目来源:
     121. Best Time to Buy and Sell Stock
    自我感觉难度/真实难度:
     
    题意:
     
    分析:
     
    自己的代码:
    class Solution:
        def maxProfit(self, prices: List[int]) -> int:
            length=len(prices)
            detalist=[prices[i+1]-prices[i] for i in range(length-1)]
            start=-1
            for k in range(length-1):
                if detalist[k]>0:
                    start=k
                    break
            if start<0 or start>length:
                return 0
            summ=0
            maxm=-1
            for i in range(start,length):
                summ+=prices[i]
                if start>maxm:
                    maxm=summ
                if summ<0:
                    summ=0
            return summ

    感觉想错了方向,原来的思路是正确的,但是以为错了,想用最大子列和来求解,后来发现有问题

    代码效率/结果:
     
    优秀代码:
    class Solution:
        def maxProfit(self, prices):
            """
            :type prices: List[int]
            :rtype: int
            """
            if len(prices) < 2:
                return 0
            min_p = prices[0]
            ma_p = 0
            for price in prices:
                if price < min_p:
                    min_p = price
                if price - min_p > ma_p:
                    ma_p = price - min_p
            return ma_p
    代码效率/结果:
     
    自己优化后的代码:
     
    反思改进策略:

    1.思路是有的,可是为什么写的很乱很差?

    2.想清楚什么是要求的量,哪些量是用来覆盖的,不断更新的

    写题时间时长:

    1hour

  • 相关阅读:
    sqhhb
    12333
    12

    今日份
    12
    彻底理解 Cookie、Session、Token
    https原理
    12312
    uiower
  • 原文地址:https://www.cnblogs.com/captain-dl/p/10568791.html
Copyright © 2020-2023  润新知