Maximum Profit from Trading Stocks with Discounts Problem
Description
LeetCode Problem 3562.
You are given an integer n, representing the number of employees in a company. Each employee is assigned a unique ID from 1 to n, and employee 1 is the CEO, is the direct or indirect boss of every employee. You are given two 1-based integer arrays, present and future, each of length n, where:
present[i]represents the current price at which thei^themployee can buy a stock today.future[i]represents the expected price at which thei^themployee can sell the stock tomorrow.
The company's hierarchy is represented by a 2D integer array hierarchy, where hierarchy[i] = [u_i, v_i] means that employee u_i is the direct boss of employee v_i.
Additionally, you have an integer budget representing the total funds available for investment.
However, the company has a discount policy: if an employee's direct boss purchases their own stock, then the employee can buy their stock at half the original price (floor(present[v] / 2)).
Return the maximum profit that can be achieved without exceeding the given budget.
Note:
- You may buy each stock at most once.
- You cannot use any profit earned from future stock prices to fund additional investments and must buy only from
budget.
Example 1:
Input: n = 2, present = [1,2], future = [4,3], hierarchy = [[1,2]], budget = 3
Output: 5
Explanation:

- Employee 1 buys the stock at price 1 and earns a profit of 4 - 1 = 3.
- Since Employee 1 is the direct boss of Employee 2, Employee 2 gets a discounted price of floor(2 / 2) = 1.
- Employee 2 buys the stock at price 1 and earns a profit of 3 - 1 = 2.
- The total buying cost is 1 + 1 = 2 <= budget. Thus, the maximum total profit achieved is 3 + 2 = 5.
Example 2:
Input: n = 2, present = [3,4], future = [5,8], hierarchy = [[1,2]], budget = 4
Output: 4
Explanation:

- Employee 2 buys the stock at price 4 and earns a profit of 8 - 4 = 4.
- Since both employees cannot buy together, the maximum profit is 4.
Example 3:
Input: n = 3, present = [4,6,8], future = [7,9,11], hierarchy = [[1,2],[1,3]], budget = 10
Output: 10
Explanation:

- Employee 1 buys the stock at price 4 and earns a profit of 7 - 4 = 3.
- Employee 3 would get a discounted price of floor(8 / 2) = 4 and earns a profit of 11 - 4 = 7.
- Employee 1 and Employee 3 buy their stocks at a total cost of 4 + 4 = 8 <= budget. Thus, the maximum total profit achieved is 3 + 7 = 10.
Example 4:
Input: n = 3, present = [5,2,3], future = [8,5,6], hierarchy = [[1,2],[2,3]], budget = 7
Output: 12
Explanation:

- Employee 1 buys the stock at price 5 and earns a profit of 8 - 5 = 3.
- Employee 2 would get a discounted price of floor(2 / 2) = 1 and earns a profit of 5 - 1 = 4.
- Employee 3 would get a discounted price of floor(3 / 2) = 1 and earns a profit of 6 - 1 = 5.
- The total cost becomes 5 + 1 + 1 = 7 <= budget. Thus, the maximum total profit achieved is 3 + 4 + 5 = 12.
Constraints:
1 <= n <= 160present.length, future.length == n1 <= present[i], future[i] <= 50hierarchy.length == n - 1hierarchy[i] == [u_i, v_i]1 <= u_i, v_i <= nu_i != v_i1 <= budget <= 160- There are no duplicate edges.
- Employee 1 is the direct or indirect boss of every employee.
- The input graph
hierarchyis guaranteed to have no cycles.
Solution
Approach: Tree DP with a Knapsack
Let dfs(u) return a table f[j][pre]: the best profit from the subtree of u when at most j budget is spent there, and pre is 1 if u's boss bought a stock (so u gets the half-price discount) and 0 otherwise. The answer is dfs(1)[budget][0].
For a node u:
- First combine the children. For each child
v, takefv = dfs(v)and merge it into a running tablenxtlike a knapsack:nxt[j][s] = max(nxt[j][s], nxt[j - jv][s] + fv[jv][s]), wherejvis the budget given tovandsis whetheruitself buys (this is theprestate the children see). - Then decide for
u. The price iscost = present[u] / (pre + 1)rounded down, and buying gainsfuture[u] - cost. Ifj < cost,ucannot buy andf[j][pre] = nxt[j][0]. Otherwisef[j][pre] = max(nxt[j][0], nxt[j - cost][1] + future[u] - cost).
Complexity: time O(n * budget^2), space O(n * budget).
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]