leetcode python3 买卖股票的最佳时机 II

代码思路:采用贪心算法思想,遍历股票价格,若当天价格比前一天的高,则保留差值视为买入,直至全部遍历完成,返回总利润

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        res=0
        for i in range(len(prices)-1):
            if prices[i]<prices[i+1]:
                res=res+prices[i+1]-prices[i]
        return res
发布了30 篇原创文章 · 获赞 0 · 访问量 305

猜你喜欢

转载自blog.csdn.net/m0_37656366/article/details/105086528