其他分享
首页 > 其他分享> > leetcode122. 买卖股票的最佳时机 II

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

标签:子串,盈利,最大,int,max,II,leetcode122,最佳时机,prices
来源: https://www.cnblogs.com/Wade-/p/14257517.html