首页 > 其他 > 详细

Leetcode 121. Best Time to Buy and Sell Stock

时间:2019-04-12 22:45:41      阅读:162      评论:0      收藏:0      [点我收藏+]

扫一遍数组,过程中维护两个值即可:一个是之前的最小值(buy),决定什么时候买.另一个是最多卖多少.

类似题目:leetcode1014

 

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        buy = float(inf)
        ret = 0
        for p in prices:
            buy = min(p, buy)
            ret = max(ret, p - buy)
        return ret

 

Leetcode 121. Best Time to Buy and Sell Stock

原文:https://www.cnblogs.com/zywscq/p/10698943.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!