leetcode122. 买卖股票的最佳时机 II

题目链接

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

题解

思路

对比leetcode121,转化为最大子串和问题
题目要求能卖多次,并且要让盈利最大。令其每天都买入卖出,记录其盈利情况。得到盈利子串,在盈利子串中求最大子串和(将大于0的部分相加),即为最大盈利额。

代码

class Solution:
    def maxProfit(self, prices: List[int]) -> int:
        T=[]
        for i in range(1,len(prices)):
            if prices[i]>prices[i-1]:
                  T.append(prices[i]-prices[i-1])
       
        max_p = sum(T)
        return max_p
posted @ 2021-01-10 10:29  deepwzh  阅读(56)  评论(0编辑  收藏  举报