파이썬 알고리즘 인터뷰 7장_배열(주식을 사고 팔기 가장 좋은 시점)

leeseungsoo0701·2022년 1월 17일
0
post-thumbnail

아이디어:

  1. 아이디어: 배열을 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

링크:
https://github.com/leeseungsoo0701/python_alogrithm/blob/main/array/leetcode/238_best_time_to_buy_and_sell_stock.py

leetcode 121

profile
한 줄이라도 정확하고 깊게 알아가보자 늦어도 좋다.

0개의 댓글

관련 채용 정보