본문 바로가기
Leetcode

121. Best Time to Buy and Sell Stock

by YGSEO 2020. 8. 10.
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

'Leetcode' 카테고리의 다른 글

70. Climbing Stairs  (0) 2020.08.15
392. Is Subsequence  (0) 2020.08.15
1025. Divisor Game  (0) 2020.08.10
856. Score of Parentheses  (0) 2020.08.10
856. Score of Parentheses  (0) 2020.08.07

댓글