Leetcode122

股票买卖的最佳时机II

问题描述

其实就是在上一个问题的基础上增加了可以多次交易这一条件
但这次我们要用贪心算法

思路

大体思路就是所有连续上涨交易日都进行买卖,连续下降交易日都不买卖这样可保证不亏

代码描述

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

你可能感兴趣的:(Leetcode122)