본문 바로가기

프로그래머/Python

[Python] Leet Code 121. Best Time to Buy and Sell Stock

본 내용은 <파이썬 알고리즘 인터뷰>를 참고했습니다.

저점과 현재 값과의 차이 계산

class Solution:
    def maxProfit(self, prices: List[int]) -> int:

        price_min = sys.maxsize
        profit = 0

        for price in prices:
            price_min = min(price_min, price)
            profit = max(profit, price - price_min)

        return profit

이 문제에서 배워야할 포인트

  • 파이썬에서 시스템상 최댓값 설정
    • sys.maxsize
    • float(inf)
  • min, max 내장함수 이용