Leetcode 121. Best Time to Buy and Sell Stock

扫一遍数组,过程中维护两个值即可:一个是之前的最小值(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

 

posted @ 2019-04-12 22:08  周洋  阅读(176)  评论(0编辑  收藏  举报