mycode  69.45%

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        res = 0
        for i in range(1,len(prices)):
            temp = prices[i] - prices[i-1]
            if temp <= 0:
                continue
            else:
                res += temp
        return res

 

参考:

下面的更快,因为索引查找的次数少一些!

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        if len(prices) == 0:
            return 0
        else:
            profit = 0 
            start = prices[0]
            for i in range(1,len(prices)):
                if prices[i]>start:
                    profit += prices[i] - start
                    start = prices[i]
                else:
                    start = prices[i]
            return profit

 

相关文章:

  • 2021-08-17
  • 2022-01-19
  • 2021-11-19
  • 2021-10-06
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-02
  • 2021-07-27
  • 2021-10-12
  • 2021-04-13
  • 2021-07-11
  • 2021-05-17
  • 2021-11-06
相关资源
相似解决方案