LeetCode 题解工作台
折扣价交易股票的最大利润
给你一个整数 n ,表示公司中员工的数量。每位员工都分配了一个从 1 到 n 的唯一 ID ,其中员工 1 是 CEO,是每一个员工的直接或间接上司。另给你两个下标从 1 开始的整数数组 present 和 future ,两个数组的长度均为 n ,具体定义如下: Create the variab…
4
题型
5
代码语言
3
相关题
当前训练重点
困难 · 二分·树·traversal
答案摘要
对每个节点 ,我们维护一个二维数组 ,表示在以 为根的子树中,预算不超过 且 的上司是否购买了股票(其中 表示购买,而 表示未购买)的情况下,可以获得的最大利润。那么答案就是 。 对节点 ,函数 返回一个 $(\text{budget}+1) \times 2$ 的二维数组 ,表示在以 为根的子树中,不超过预算 且 的上司是否购买了股票的情况下,可以获得的最大利润。
Interview AiBoxInterview AiBox 实时 AI 助手,陪你讲清 二分·树·traversal 题型思路
题目描述
给你一个整数 n,表示公司中员工的数量。每位员工都分配了一个从 1 到 n 的唯一 ID ,其中员工 1 是 CEO,是每一个员工的直接或间接上司。另给你两个下标从 1 开始的整数数组 present 和 future,两个数组的长度均为 n,具体定义如下:
present[i]表示第i位员工今天可以购买股票的 当前价格 。future[i]表示第i位员工明天可以卖出股票的 预期价格 。
公司的层级关系由二维整数数组 hierarchy 表示,其中 hierarchy[i] = [ui, vi] 表示员工 ui 是员工 vi 的直属上司。
此外,再给你一个整数 budget,表示可用于投资的总预算。
公司有一项折扣政策:如果某位员工的直属上司购买了公司的股票,那么该员工可以以 半价 购买股票(即 floor(present[v] / 2))。
请返回在不超过给定预算的情况下可以获得的 最大利润 。
注意:
- 每只股票最多只能购买一次。
- 不能使用股票未来的收益来增加投资预算,购买只能依赖于
budget。
示例 1:
输入: n = 2, present = [1,2], future = [4,3], hierarchy = [[1,2]], budget = 3
输出: 5
解释:

- 员工 1 以价格 1 购买股票,获得利润
4 - 1 = 3。 - 由于员工 1 是员工 2 的直属上司,员工 2 可以以折扣价
floor(2 / 2) = 1购买股票。 - 员工 2 以价格 1 购买股票,获得利润
3 - 1 = 2。 - 总购买成本为
1 + 1 = 2 <= budget,因此最大总利润为3 + 2 = 5。
示例 2:
输入: n = 2, present = [3,4], future = [5,8], hierarchy = [[1,2]], budget = 4
输出: 4
解释:

- 员工 2 以价格 4 购买股票,获得利润
8 - 4 = 4。 - 由于两位员工无法同时购买,最大利润为 4。
示例 3:
输入: n = 3, present = [4,6,8], future = [7,9,11], hierarchy = [[1,2],[1,3]], budget = 10
输出: 10
解释:

- 员工 1 以价格 4 购买股票,获得利润
7 - 4 = 3。 - 员工 3 可获得折扣价
floor(8 / 2) = 4,获得利润11 - 4 = 7。 - 员工 1 和员工 3 的总购买成本为
4 + 4 = 8 <= budget,因此最大总利润为3 + 7 = 10。
示例 4:
输入: n = 3, present = [5,2,3], future = [8,5,6], hierarchy = [[1,2],[2,3]], budget = 7
输出: 12
解释:

- 员工 1 以价格 5 购买股票,获得利润
8 - 5 = 3。 - 员工 2 可获得折扣价
floor(2 / 2) = 1,获得利润5 - 1 = 4。 - 员工 3 可获得折扣价
floor(3 / 2) = 1,获得利润6 - 1 = 5。 - 总成本为
5 + 1 + 1 = 7 <= budget,因此最大总利润为3 + 4 + 5 = 12。
提示:
1 <= n <= 160present.length, future.length == n1 <= present[i], future[i] <= 50hierarchy.length == n - 1hierarchy[i] == [ui, vi]1 <= ui, vi <= nui != vi1 <= budget <= 160- 没有重复的边。
- 员工 1 是所有员工的直接或间接上司。
- 输入的图
hierarchy保证 无环 。
解题思路
方法一:树形动态规划
对每个节点 ,我们维护一个二维数组 ,表示在以 为根的子树中,预算不超过 且 的上司是否购买了股票(其中 表示购买,而 表示未购买)的情况下,可以获得的最大利润。那么答案就是 。
对节点 ,函数 返回一个 的二维数组 ,表示在以 为根的子树中,不超过预算 且 的上司是否购买了股票的情况下,可以获得的最大利润。
对 ,我们要考虑两件事:
- 节点 本身是否买股票(会占用一部分预算 ,其中 )。并增加利润 。
- 节点 的子节点 如何分配预算以最大化利润。我们把每个子节点的 看成“物品”,用背包把子树的利润合并到当前 的 数组中。
具体实现时,我们先初始化一个 的二维数组 ,表示当前已经合并了子节点的利润。然后对于每个子节点 ,我们递归调用 得到子节点的利润数组 ,并用背包把 合并到 中。
合并公式为:
其中 表示分配给子节点 的预算。
合并完所有子节点后的 表示在 本身尚未决定是否购买股票的情况下,且 的上次购买状态为 时,把预算 全部用于子节点所能获得的最大利润。
最后,我们决定 是否购买股票。
- 如果 ,则 无法购买股票,此时 。
- 如果 ,则 可以选择购买或不购买股票,此时 。
最后返回 即可。
答案为 。
时间复杂度 ,空间复杂度 。
class Solution:
def maxProfit(
self,
n: int,
present: List[int],
future: List[int],
hierarchy: List[List[int]],
budget: int,
) -> int:
max = lambda a, b: a if a > b else b
g = [[] for _ in range(n + 1)]
for u, v in hierarchy:
g[u].append(v)
def dfs(u: int):
nxt = [[0, 0] for _ in range(budget + 1)]
for v in g[u]:
fv = dfs(v)
for j in range(budget, -1, -1):
for jv in range(j + 1):
for pre in (0, 1):
val = nxt[j - jv][pre] + fv[jv][pre]
if val > nxt[j][pre]:
nxt[j][pre] = val
f = [[0, 0] for _ in range(budget + 1)]
price = future[u - 1]
for j in range(budget + 1):
for pre in (0, 1):
cost = present[u - 1] // (pre + 1)
if j >= cost:
f[j][pre] = max(nxt[j][0], nxt[j - cost][1] + (price - cost))
else:
f[j][pre] = nxt[j][0]
return f
return dfs(1)[budget][0]
复杂度分析
| 指标 | 值 |
|---|---|
| 时间 | complexity is O(n * budget^2) due to DFS traversal and DP merging of children states for each budget allocation. Space complexity is O(n * budget) for storing DP states per node. |
| 空间 | Depends on the final approach |
面试官常问的追问
外企场景- question_mark
They may ask you to track two DP states per node to handle discounts effectively.
- question_mark
Expect a prompt on merging child node profits under budget constraints.
- question_mark
Clarification on handling hierarchical dependencies versus independent investments may be requested.
常见陷阱
外企场景- error
Ignoring the distinction between max_profit and max_profit1 leads to incorrect discount calculations.
- error
Merging child DP states incorrectly can underestimate total achievable profit.
- error
Not respecting the tree structure can cause invalid profit combinations violating hierarchy rules.
进阶变体
外企场景- arrow_right_alt
Allowing multiple discount types per employee requiring additional DP states.
- arrow_right_alt
Changing the hierarchy to a forest of trees instead of a single CEO-rooted tree.
- arrow_right_alt
Introducing a cost for applying discounts, modifying profit calculations per node.