Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save inspirit941/ee5a46a3b4ea8883f61e06426b5d32ad to your computer and use it in GitHub Desktop.
Save inspirit941/ee5a46a3b4ea8883f61e06426b5d32ad to your computer and use it in GitHub Desktop.
import math
class Solution:
def maxProfit(self, prices: List[int]) -> int:
# 최솟값을 매번 갱신하면서
# 현재 값과 최솟값과의 차이를 구해 최댓값을 저장하도로 한다
# 카데인(Kadane) 알고리즘으로, O(n) 에 풀이 가능
profit = 0
min_price = math.inf
for price in prices:
min_price = min(price, min_price)
profit = max(profit, price - min_price)
return profit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment