class Solution:
def maxProfit(self, prices: List[int]) -> int:
result = 0
for i in range(len(prices)):
for j in range(i + 1, len(prices)):
if prices[j] - prices[i] > result:
result = prices[j] - prices[i]
return result
class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = 0
min_price = sys.maxsize
# 최솟값과 최댓값을 계속 갱신
for price in prices:
min_price = min(min_price, price)
profit = max(profit, price - min_price)
return profit
runtime : 68 ms
서적 파이썬 알고리즘 인터뷰 - 박상길
을 참고하였습니다.