根据力扣121改编的题目,本文使用遍历整个列表的方法,直接解出。
class Solution:
def maxProfit(self, prices: List[int]) -> int:
if not prices: # 如果价格列表为空,则直接返回0
return 0
sum_total = 0
for i in range(len(prices)-1):
if prices[i+1] - prices[i] > 0:
sum_total += prices[i+1] - prices[i]
return sum_total