不忘初心,
牢记使命。

leetcode121-股票买卖

2021-05-12 大聪明 0评论 145 0喜欢

链接:https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock。

class Solution(object):
    def maxProfit(self, prices):
        """
        :type prices: List[int]
        :rtype: int
        """
        # 贪心的思想,只要买入后第二天卖出赚钱,那就买,最后把利润累加
        min_price = max(prices)
        max_profit = 0
        for p in prices:
            if p < min_price:
                min_price = p
            elif p - min_price > max_profit:
                max_profit = p - min_price
        return max_profit


solution = Solution()
# r = solution.maxProfit([7,1,5,3,6,4])
r = solution.maxProfit([2,5,1,3])
# r = solution.maxProfit([7,6,4,3,1])
print(r)

发表评论 取消回复

电子邮件地址不会被公开。

请输入正确格式的qq邮箱
请输入以http或https开头的URL,格式如:https://libo_sober.top