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: 12
💡 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: 5+4+3 = 12
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: 8
💡 Note: Buy both: CEO (cost 8, profit 4) and Employee 2 with discount (cost 3, profit 4). Total: 8 profit.

Constraints

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

Visualization

Tap to expand
Stock Trading with Company Hierarchy DiscountsInput: HierarchyCEO (1) → Emp 2, Emp 3Present: [10,5,3]Future: [15,8,7]Budget: 15Process: Apply DiscountsIf CEO buys → Emp costs halvedEmp 2: 5 → 2, Emp 3: 3 → 1Try all combinationsStay within budgetOutputMax Profit: 12Buy: CEO + Emp 3Cost: 10 + 1 = 11Profit: 5 + 4 + 3 = 12Key Algorithm Steps1. Build hierarchy tree from boss-subordinate pairs2. Use DP: for each employee, decide buy vs don't buy3. If parent buys, child gets 50% discount on their stock4. Maximize profit while staying within budget constraint5. Use memoization to cache subproblem results🎯 Key Insight: Discount policy creates dependency between parent and child decisions
Understanding the Visualization
1
Company Hierarchy
Build tree from boss-subordinate relationships
2
Discount Policy
Boss purchase gives 50% discount to subordinates
3
Optimization
Find maximum profit within budget constraint
Key Takeaway
🎯 Key Insight: The discount policy creates interdependence between employee decisions in the hierarchy tree
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