题目链接

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

相关文章:

  • 2021-12-01
  • 2022-01-01
  • 2021-09-22
猜你喜欢
  • 2021-06-22
  • 2022-12-23
  • 2021-10-22
相关资源
相似解决方案