121. Best Time to Buy and Sell Stock

kukudas·2022년 2월 28일
0

Algorithm

목록 보기
10/46
class Solution:
    def maxProfit(self, prices):
        cur_profit = 0
        cur_low = prices[0]

        # 시간이 뒤로만 가니까 앞에서부터 한 번만 보면됨
        for price in prices:
            # 지금이 고점이면 수익 갱신해줌.
            if price - cur_low > cur_profit:
                cur_profit = price - cur_low
                
            # 저점 갱신
            if cur_low > price:
                cur_low = price

        return cur_profit

출처

0개의 댓글

관련 채용 정보