美文网首页
leetcode 714 买卖股票的最佳时机含手续费

leetcode 714 买卖股票的最佳时机含手续费

作者: Arsenal4ever | 来源:发表于2020-01-20 01:01 被阅读0次

这题好难思考。。。根据以前买股票经验,只需要将手续费减掉就好。这里有个思想,就是转换,如手续费为2, [3, 8, 9],算 3 到 9很麻烦,可先算 3 到 8,在算 8 到 9,最后等同于 3 到 9,这就需要条件转换了。

class Solution(object):
    def maxProfit(self, prices, fee):
        """
        :type prices: List[int]
        :type fee: int
        :rtype: int
        """
        answer = 0
        buyPrice = float('inf')
        for price in prices:
            if price < buyPrice:
                buyPrice = price
                continue
            if price > buyPrice + fee:
                answer += price - buyPrice - fee
                buyPrice = price - fee
        return answer

相关文章

网友评论

      本文标题:leetcode 714 买卖股票的最佳时机含手续费

      本文链接:https://www.haomeiwen.com/subject/vnwdzctx.html