Skip to content

Commit a0239dd

Browse files
authored
feat: add solution to lc problem: No.3562 (#4903)
1 parent 1b5bafc commit a0239dd

File tree

2 files changed

+66
-2
lines changed

2 files changed

+66
-2
lines changed

solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,39 @@ tags:
143143

144144
<!-- solution:start -->
145145

146-
### 方法一
146+
### 方法一:树形动态规划
147+
148+
对每个节点 $u$,我们维护一个二维数组 $f_u[j][pre]$,表示在以 $u$ 为根的子树中,预算不超过 $j$ 且 $u$ 的上司是否购买了股票(其中 $pre=1$ 表示购买,而 $pre=0$ 表示未购买)的情况下,可以获得的最大利润。那么答案就是 $f_1[\text{budget}][0]$。
149+
150+
对节点 $u$,函数 $\text{dfs}(u)$ 返回一个 $(\text{budget}+1) \times 2$ 的二维数组 $f$,表示在以 $u$ 为根的子树中,不超过预算 $j$ 且 $u$ 的上司是否购买了股票的情况下,可以获得的最大利润。
151+
152+
对 $u$,我们要考虑两件事:
153+
154+
1. 节点 $u$ 本身是否买股票(会占用一部分预算 $\text{cost}$,其中 $\text{cost} = \lfloor \text{present}[u] / (pre + 1) \rfloor$)。并增加利润 $\text{future}[u] - \text{cost}$。
155+
2. 节点 $u$ 的子节点 $v$ 如何分配预算以最大化利润。我们把每个子节点的 $\text{dfs}(v)$ 看成“物品”,用背包把子树的利润合并到当前 $u$ 的 $\text{nxt}$ 数组中。
156+
157+
具体实现时,我们先初始化一个 $(\text{budget}+1) \times 2$ 的二维数组 $\text{nxt}$,表示当前已经合并了子节点的利润。然后对于每个子节点 $v$,我们递归调用 $\text{dfs}(v)$ 得到子节点的利润数组 $\text{fv}$,并用背包把 $\text{fv}$ 合并到 $\text{nxt}$ 中。
158+
159+
合并公式为:
160+
161+
$$
162+
\text{nxt}[j][pre] = \max(\text{nxt}[j][pre], \text{nxt}[j - j_v][pre] + \text{fv}[j_v][pre])
163+
$$
164+
165+
其中 $j_v$ 表示分配给子节点 $v$ 的预算。
166+
167+
合并完所有子节点后的 $\text{nxt}[j][pre]$ 表示在 $u$ 本身尚未决定是否购买股票的情况下,且 $u$ 的上次购买状态为 $pre$ 时,把预算 $j$ 全部用于子节点所能获得的最大利润。
168+
169+
最后,我们决定 $u$ 是否购买股票。
170+
171+
- 如果 $j \lt \text{cost}$,则 $u$ 无法购买股票,此时 $f[j][pre] = \text{nxt}[j][0]$。
172+
- 如果 $j \geq \text{cost}$,则 $u$ 可以选择购买或不购买股票,此时 $f[j][pre] = \max(\text{nxt}[j][0], \text{nxt}[j - \text{cost}][1] + (\text{future}[u] - \text{cost}))$。
173+
174+
最后返回 $f$ 即可。
175+
176+
答案为 $\text{dfs}(1)[\text{budget}][0]$。
177+
178+
时间复杂度 $O(n \times \text{budget}^2)$,空间复杂度 $O(n \times \text{budget})$。
147179

148180
<!-- tabs:start -->
149181

solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README_EN.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,39 @@ tags:
140140

141141
<!-- solution:start -->
142142

143-
### Solution 1
143+
### Solution 1: Tree Dynamic Programming
144+
145+
For each node $u$, we maintain a 2D array $f_u[j][pre]$, representing the maximum profit that can be obtained in the subtree rooted at $u$ with a budget not exceeding $j$ and whether $u$'s manager purchased stocks (where $pre=1$ means purchased, and $pre=0$ means not purchased). The answer is $f_1[\text{budget}][0]$.
146+
147+
For node $u$, the function $\text{dfs}(u)$ returns a $(\text{budget}+1) \times 2$ 2D array $f$, representing the maximum profit that can be obtained in the subtree rooted at $u$ with a budget not exceeding $j$ and whether $u$'s manager purchased stocks.
148+
149+
For $u$, we need to consider two things:
150+
151+
1. Whether node $u$ itself buys stocks (which will consume part of the budget $\text{cost}$, where $\text{cost} = \lfloor \text{present}[u] / (pre + 1) \rfloor$), and increase the profit by $\text{future}[u] - \text{cost}$.
152+
2. How to allocate the budget among node $u$'s child nodes $v$ to maximize profit. We treat each child node's $\text{dfs}(v)$ as an "item" and use a knapsack approach to merge the subtree profits into the current $u$'s $\text{nxt}$ array.
153+
154+
In the specific implementation, we first initialize a $(\text{budget}+1) \times 2$ 2D array $\text{nxt}$, representing the profits that have been merged from child nodes. Then for each child node $v$, we recursively call $\text{dfs}(v)$ to get the profit array $\text{fv}$ of the child node, and use a knapsack approach to merge $\text{fv}$ into $\text{nxt}$.
155+
156+
The merge formula is:
157+
158+
$$
159+
\text{nxt}[j][pre] = \max(\text{nxt}[j][pre], \text{nxt}[j - j_v][pre] + \text{fv}[j_v][pre])
160+
$$
161+
162+
where $j_v$ represents the budget allocated to child node $v$.
163+
164+
After merging all child nodes, $\text{nxt}[j][pre]$ represents the maximum profit that can be obtained by allocating the entire budget $j$ to child nodes when $u$ itself has not yet decided whether to buy stocks, and $u$'s manager's purchase state is $pre$.
165+
166+
Finally, we decide whether $u$ purchases stocks.
167+
168+
- If $j \lt \text{cost}$, then $u$ cannot purchase stocks, in which case $f[j][pre] = \text{nxt}[j][0]$.
169+
- If $j \geq \text{cost}$, then $u$ can choose to purchase or not purchase stocks, in which case $f[j][pre] = \max(\text{nxt}[j][0], \text{nxt}[j - \text{cost}][1] + (\text{future}[u] - \text{cost}))$.
170+
171+
Finally, return $f$.
172+
173+
The answer is $\text{dfs}(1)[\text{budget}][0]$.
174+
175+
The time complexity is $O(n \times \text{budget}^2)$, and the space complexity is $O(n \times \text{budget})$.
144176

145177
<!-- tabs:start -->
146178

0 commit comments

Comments
 (0)