1. leetcode每日一题
哈哈哈今天依旧是股票问题,这回基本有思路了,有进步嘿嘿,继续努力。
其实含手续费的问题和不含手续费的问题,区别只是在于,卖出的时候多减去一个fee
class Solution(object):
def maxProfit(self, prices, fee):
"""
:type prices: List[int]
:type fee: int
:rtype: int
"""
if not prices:
return 0
n = len(prices)
# dp[i][0]表示第i天结束时不持有股票
# dp[i][1]表示第i天结束后持有股票
dp = [[0, -prices[0]]] + [[0]*2 for _ in range(n-1)]
for i in range(1,n):
# 前一天不持有;前一天持有,i天卖了
dp[i][0] = max(dp[i-1][0], dp[i-1][1] + prices[i] - fee)
# 前一天持有;其前一天不持有,今天买了
dp[i][1] = max(dp[i-1][1], dp[i-1][0] - prices[i])
return dp[n-1][0]