Maximum Profit from Trading Stocks with Discounts - Problem

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 who 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 the i-th employee can buy a stock today
  • future[i] represents the expected price at which the i-th employee 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.

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.

Input & Output

Example 1 — Basic Hierarchy
$ Input: n = 3, present = [10,5,3], future = [15,8,7], hierarchy = [[1,2],[1,3]], budget = 15
Output: 9
💡 Note: Buy CEO's stock (cost 10, profit 5) and Employee 3's stock (cost 1 with discount, profit 4). Total cost: 11, total profit: 9
Example 2 — Limited Budget
$ Input: n = 3, present = [20,10,5], future = [25,15,10], hierarchy = [[1,2],[1,3]], budget = 10
Output: 5
💡 Note: Can only buy Employee 3's stock (cost 5, profit 5). CEO's stock is too expensive.
Example 3 — Maximum Discount
$ Input: n = 2, present = [8,6], future = [12,10], hierarchy = [[1,2]], budget = 10
Output: 4
💡 Note: Can only buy Employee 2's stock (cost 6, profit 4) within budget. CEO + discounted Employee 2 would cost 11 > 10.

Constraints

  • 1 ≤ n ≤ 1000
  • 1 ≤ present[i], future[i] ≤ 104
  • 1 ≤ budget ≤ 105
  • hierarchy.length = n - 1

Visualization

Tap to expand
Maximum Profit from Trading Stocks INPUT Company Hierarchy (Tree) 1 CEO 2 3 present[] (buy price) 10 5 3 future[] (sell price) 15 8 7 [1] [2] [3] Budget = 15 Profit[i] = future[i] - present[i] [5, 3, 4] ALGORITHM (Tree DP) 1 Define DP States dp[node][budget][boss_bought] Track if parent bought stock 2 Apply Discount Rule If boss buys: cost = price/2 Emp2: 5-->2, Emp3: 3-->1 3 DFS from Leaves Combine child subtree results Knapsack merge at each node 4 Choose Best Option Buy/Skip at each employee Optimal Selection: Buy Emp1: cost=10, profit=5 Buy Emp2: cost=2 (disc), p=3 Buy Emp3: cost=1 (disc), p=4 Total: 10+2+1=13 <= 15 [OK] FINAL RESULT Stocks Purchased: Emp 1 $10 Emp 2 $2 Emp 3 $1 50% 50% Profit Calculation Emp1: 15 - 10 = 5 Emp2: 8 - 5 = 3 Emp3: 7 - 3 = 4 Max Profit 12 Budget used: 13/15 [OK] Key Insight: Tree DP with parent-child dependency: When boss buys stock, children get 50% discount. Use knapsack-style DP to merge subtree results. Track two states at each node: boss bought / boss skipped. Time: O(n * budget^2) | Space: O(n * budget) | The discount incentivizes buying from top-down. TutorialsPoint - Maximum Profit from Trading Stocks with Discounts | DP Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
22.3K Views
Medium Frequency
~35 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen