[리트코드] Best Time to Buy and Sell Stock II

박형진·2021년 12월 30일
0

https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii/


1. 전체 코드

from typing import List


class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        result = 0
        for i in range(len(prices) - 1):
            if prices[i + 1] > prices[i]:
                result += prices[i + 1] - prices[i]
        return result

2. 후기

이 문제에서 탐욕적인 접근은 현재 인덱스보다 바로 다음 인덱스의 prices 값이 더 큰 경우 하나만 고려한다.

profile
안녕하세요!

0개의 댓글