Max Profit (python)

cellenar26·2020년 10월 1일
0

def maxProfit(prices):
n = len(prices)
maxPro = 0
minPrice = prices[0]
maxProIndex = 0;
minPriceIndex = 0;
for x in range(1, n):
profit = prices[x] - minPrice
if profit > maxPro:
maxPro = profit
maxProIndex = x;

    if prices[x] < minPrice:
        minPrice = prices[x]
        minPriceIndex = x;

print("maxProfitIndex : ",maxProIndex);
print("minPriceIndex : ",minPriceIndex);
return maxPro

stock = [10300, 9600, 9800, 8200, 7800, 8300, 9500, 9800, 10200, 9500]
print(maxProfit(stock))

출처 : https://velog.io/@happykimnh/%EB%AA%A8%EB%91%90%EC%9D%98-%EC%95%8C%EA%B3%A0%EB%A6%AC%EC%A6%98-10.-Max-Profit

profile
Mr.SQL velog에 오신것을 환영합니다.

0개의 댓글