class Solution:
def maxProfit(self, prices: List[int]) -> int:
profit = max_p = 0
for i in range(1, len(prices)) :
current_profit = prices[i] - prices[i-1]
profit = max(current_profit, profit+current_profit)
max_p = max(max_p, profit)
return max_p
처음 문제를 보자마자 maxSubarray
(?)에서 풀었던 Kadane's Algorithm
이 생각났다.
그래서 현재 금액에서 전 날 금액을 뺀 이익 금액을 current_profit
으로 잡았고 profit
을 갱신했다.
그랬을 때 최대 이익인 max_p
와 비교해여 최대 이익 리턴