UOJ 買賣最大化利潤

Problem description:

 

Solution:

def maxProfit(self, prices: List[int]) -> int:
        #return sum(y-x for y,x in zip(prices[1:],prices[:-1]) if y-x>0)
        if len(prices)<1:
            return 0
        buyPrice,res = prices[0],0
        for price in prices:
            if price>=buyPrice:
                res+=price - buyPrice
                buyPrice = price
            else:
                buyPrice = price
        return res

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章