아이디어:
- 아이디어: 배열을 for 문으로 하나씩 조회하며 저점을 갱신하며 현재 값과의 차이를 저장하여 그 차이 중에서 가장 큰 값을 return 해주면 된다.
""" 주식을 사고 팔기 가장 좋은 시점 문제 : 한 번의 거래로 낼 수 있는 최대 이익을 산출하라 입력: [7,1,5,3,6,4] 출력: 5 """
def sellStock(inputs: list[int])->int:
lowest = prices[0]
profit = 0
for price in prices:
if price < lowest:
lowest = price
if profit < (price - lowest):
profit = price - lowest
return profit
leetcode 링크:
https://leetcode.com/problems/best-time-to-buy-and-sell-stock
leetcode 121