Skip to content

Latest commit

 

History

History
18 lines (12 loc) · 443 Bytes

714.-best-time-to-buy-and-sell-stock-with-transaction-fee.md

File metadata and controls

18 lines (12 loc) · 443 Bytes

714. Best Time to Buy and Sell Stock with Transaction Fee

class Solution:
    def maxProfit(self, prices: List[int], fee: int) -> int:

        n = len(prices)
        dp = [[0,0] for _ in range(n)]


        dp[0][1] = - prices[0]
        for i in range(1, n):
            dp[i][0] = max(prices[i] - fee + dp[i-1][1], dp[i-1][0])
            dp[i][1] = max( - prices[i] + dp[i-1][0], dp[i-1][1])

        return max(dp[-1])