首页 > 其他 > 详细

leetcode123.买股票的最佳时机3

时间:2019-09-15 13:13:40      阅读:81      评论:0      收藏:0      [点我收藏+]

只能买卖两次

1.二分法,超时
class Solution:
    def max_one_profit(self,prices):
        if not prices:
            return 0
        
        max_profit=0
        min_price=prices[0]
        for i in range(1,len(prices)):
            if prices[i]<min_price:
                min_price=prices[i]
            if prices[i]-min_price>max_profit:
                max_profit=prices[i]-min_price
        return max_profit
    
    def maxProfit(self, prices: List[int]) -> int:
        max_profit=0
        for i in range(len(prices)):
            left=prices[:i]
            right=prices[i:]
            max_p1=self.max_one_profit(left)
            max_p2=self.max_one_profit(right)
            if max_p1+max_p2>max_profit:
                max_profit=max_p1+max_p2
        return max_profit

2.动态规划

leetcode123.买股票的最佳时机3

原文:https://www.cnblogs.com/liguitar/p/11521669.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!