알고리즘/LeetCode
122. Best Time to Buy and Sell Stock II
bright_code
2020. 10. 5. 08:22
728x90
반응형
class Solution:
def maxProfit(self, prices: List[int]) -> int:
result = 0
for i in range(len(prices)-1):
if prices[i] < prices[i+1]:
result += prices[i+1] - prices[i]
return result
쌀 때 사서 비쌀 때 팔아 가장 높은 수익을 얻는 문제.
그리디 알고리즘을 사용하여 풀면 간단하다.
만약, 현재보다 다음 값이 오른다면 사고 판다.
leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/
Best Time to Buy and Sell Stock II - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
728x90
반응형