快轉到主要內容
  1. LeetCode/

LeetCode 714: Best Time to Buy and Sell Stock with Transaction Fee

·2 分鐘· ·
LeetCode Medium Dynamic-Programming State-Machine
Wei Yi Chung
作者
Wei Yi Chung
Working at the contributing of open source, distributed systems, and data engineering.
目錄

基本資料
#

難易度: medium 第一次嘗試:2026-05-17 來源:Day 23 learning note

學習脈絡
#

這篇是從 learning note 裡該 LeetCode 題目的段落重新整理出來的版本。我保留當天筆記中的修正點、比較點、容易犯錯的地方,並移除同一天其他非 LeetCode 主題,避免文章內容混題。

筆記中提到的相關提醒
#

  • derive LC 714 from hold/cash and explain where the fee is charged

當天筆記摘錄
#

Problem 2 - LC 714 Best Time to Buy and Sell Stock with Transaction Fee
#

  • Pattern: 2-state stock DP with transaction cost.

Why This Fits
#

The legal actions are the same as unlimited transactions:

  • keep holding or buy
  • keep cash or sell

The only change is:

every completed transaction pays a fee

So the state model stays simple, but one transition absorbs the fee.

Core State / Invariant
#

hold = best profit after day i while holding one stock
cash = best profit after day i while not holding stock

Transitions
#

Charge the fee on sell:

hold = max(previous_hold, previous_cash - price)
cash = max(previous_cash, previous_hold + price - fee)

Equivalent formulations can charge on buy instead. The important thing is consistency.

Complexity
#

Time: O(n)
Space: O(1)

Common Mistakes
#

  • subtracting the fee on both buy and sell
  • mixing two equivalent formulations and double-charging
  • calling it greedy without explaining the state meaning
  • updating cash from an already-updated hold instead of previous_hold

Strong Spoken Explanation
#

This is still a hold/cash state machine. The fee does not create a new legal state; it only changes the economics of selling. So I keep the same two-state model as Stock II and subtract the fee in the sell transition. That keeps the recurrence clean and the interpretation stable.

Implementation Warning
#

When writing the rolling version, preserve previous-day values explicitly:

prev_hold = hold
prev_cash = cash

Then update from those previous states, not from already-mutated same-day values.

正確解法
#

上面的筆記保留了推理脈絡和當天需要修正的點。下面是我會提交的版本。

from typing import List

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

        for price in prices[1:]:
            hold = max(hold, cash - price)
            cash = max(cash, hold + price - fee)

        return cash

複雜度
#

Time O(n), Space O(1).

要特別避免的錯誤
#

  • Subtracting the fee on both buy and sell.
  • Using cooldown logic; there is no cooldown here.

面試口說整理
#

先講清楚 state definition,再說 transition 為什麼維持這個 state。只要這題有 loop direction、狀態壓縮、或題型相似但 answer shape 不同的地方,就要主動講出來,因為那通常就是這類題最容易出錯的點。

相關文章

LeetCode 309: Best Time to Buy and Sell Stock with Cooldown
·2 分鐘
LeetCode Medium Dynamic-Programming State-Machine
LeetCode 309 解題筆記,依照原始 learning note 重新整理
LeetCode 122: Best Time To Buy And Sell Stock II
·2 分鐘
LeetCode Medium Dynamic-Programming Greedy State-Machine
LeetCode 122 解題筆記,依照原始 learning note 重新整理
LeetCode 123: Best Time to Buy and Sell Stock III
·3 分鐘
LeetCode Hard Dynamic-Programming State-Machine
LeetCode 123 解題筆記,依照原始 learning note 重新整理
LeetCode 188: Best Time to Buy and Sell Stock IV
·3 分鐘
LeetCode Hard Dynamic-Programming State-Machine
LeetCode 188 解題筆記,依照原始 learning note 重新整理
LeetCode 413: Arithmetic Slices
·3 分鐘
LeetCode Medium Dynamic-Programming
LeetCode 413 解題筆記,依照原始 learning note 重新整理
LeetCode 53: Maximum Subarray
·2 分鐘
LeetCode Medium Dynamic-Programming Kadane
LeetCode 53 解題筆記,依照原始 learning note 重新整理