Open fockspaces opened 10 months ago
this actually comes from 2D dp, we create two state "one_stock" and "no_stock" for update
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
n = len(prices)
with_no = [0 for _ in range(n)]
with_one = [0 for _ in range(n)]
with_no[0], with_one[0] = 0, -prices[0]
for i in range(1, n):
with_no[i] = max(with_no[i - 1], with_one[i - 1] + prices[i] - fee)
with_one[i] = max(with_one[i - 1], with_no[i - 1] - prices[i])
return with_no[-1]
this is a tricky problem, keyword : state transfer maintain the two variables