这题好难思考。。。根据以前买股票经验,只需要将手续费减掉就好。这里有个思想,就是转换,如手续费为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
网友评论