LeetCode 题解工作台
买卖股票的最佳时机含手续费
给定一个整数数组 prices ,其中 prices[i] 表示第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。 你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。 返回获得利润的最大值。 注意: 这里的一笔交易指买…
3
题型
5
代码语言
3
相关题
当前训练重点
中等 · 状态·转移·动态规划
答案摘要
我们设计一个函数 $dfs(i, j)$,表示从第 天开始,状态为 时,能够获得的最大利润。其中 的取值为 $0, 1$,分别表示当前不持有股票和持有股票。答案即为 $dfs(0, 0)$。 函数 $dfs(i, j)$ 的执行逻辑如下:
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 状态·转移·动态规划 题型思路
题目描述
给定一个整数数组 prices,其中 prices[i]表示第 i 天的股票价格 ;整数 fee 代表了交易股票的手续费用。
你可以无限次地完成交易,但是你每笔交易都需要付手续费。如果你已经购买了一个股票,在卖出它之前你就不能再继续购买股票了。
返回获得利润的最大值。
注意:这里的一笔交易指买入持有并卖出股票的整个过程,每笔交易你只需要为支付一次手续费。
示例 1:
输入:prices = [1, 3, 2, 8, 4, 9], fee = 2 输出:8 解释:能够达到的最大利润: 在此处买入 prices[0] = 1 在此处卖出 prices[3] = 8 在此处买入 prices[4] = 4 在此处卖出 prices[5] = 9 总利润: ((8 - 1) - 2) + ((9 - 4) - 2) = 8
示例 2:
输入:prices = [1,3,7,5,10,3], fee = 3 输出:6
提示:
1 <= prices.length <= 5 * 1041 <= prices[i] < 5 * 1040 <= fee < 5 * 104
解题思路
方法一:记忆化搜索
我们设计一个函数 ,表示从第 天开始,状态为 时,能够获得的最大利润。其中 的取值为 ,分别表示当前不持有股票和持有股票。答案即为 。
函数 的执行逻辑如下:
如果 ,那么没有股票可以交易了,此时返回 ;
否则,我们可以选择不交易,此时 。我们也可以进行股票交易,如果此时 ,说明当前持有股票,可以卖出,此时 ;如果此时 ,说明当前不持有股票,可以买入,此时 。取最大值作为函数 的返回值。
答案为 。
为了避免重复计算,我们使用记忆化搜索的方法,用一个数组 记录 的返回值,如果 不为 ,说明已经计算过,直接返回 即可。
时间复杂度 ,空间复杂度 。其中 为数组 的长度。
class Solution:
def maxProfit(self, prices: List[int], fee: int) -> int:
@cache
def dfs(i: int, j: int) -> int:
if i >= len(prices):
return 0
ans = dfs(i + 1, j)
if j:
ans = max(ans, prices[i] + dfs(i + 1, 0) - fee)
else:
ans = max(ans, -prices[i] + dfs(i + 1, 1))
return ans
return dfs(0, 0)
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n) because each price is processed once, updating constant state variables. Space complexity is O(1) since only two variables are maintained regardless of input size. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
Look for a clear state representation separating holding vs not holding stock.
- question_mark
Expect efficient O(n) solution rather than nested loops for multiple transactions.
- question_mark
Check for correct handling of the transaction fee on each sell operation.
常见陷阱
外企场景- error
Forgetting to subtract the transaction fee when updating cash after a sell.
- error
Using extra arrays for all states, increasing space complexity unnecessarily.
- error
Mixing the order of updates for hold and cash, leading to incorrect state transitions.
进阶变体
外企场景- arrow_right_alt
Restrict number of transactions to K and adapt the state transition DP accordingly.
- arrow_right_alt
Introduce cooldown periods after selling, requiring additional state tracking.
- arrow_right_alt
Allow multiple stocks with independent fees, extending the DP state dimensions.