当前位置: 首页 > 后端技术 > Python

力扣122.买卖股票的最佳时间IIPython实现

时间:2023-03-26 18:11:15 Python

题目要求:思路:每天买卖股票,前一天卖出,当天再买入,维持一个res保存总利润如果股价当日高于前一天的高点,即有盈利,把这个盈利加到res,如果没有盈利,就给res加0,返回res核心代码:res=0foriinrange(1,len(prices)):res+=max(0,prices[i]-prices[i-1])returnres完整代码:类解决方案:defmaxProfit(self,prices:List[int])->int:res=0foriinrange(1,len(prices)):res+=max(0,prices[i]-prices[i-1])returnres