Leetcode

121. Best Time to Buy and Sell Stock

YGSEO 2020. 8. 10. 18:07
728x90
class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        if not prices: return 0
        sell_prc = max(prices)
        max_prf = 0
        for p in prices:
            if p < sell_prc: 
                sell_prc = p # find the min value
            max_prf = max(p - sell_prc, max_prf) # compare current profit with max profit
            
        return max_prf

출처: leetcode.com/problems/best-time-to-buy-and-sell-stock/discuss/400573/Python3-beats-93.61-with-comments

 

728x90