Maximum Profit from Trading Stocks with Discounts - Problem

Imagine you're the chief investment officer at a company with a hierarchical structure where the CEO (employee 1) is at the top, and every other employee reports to someone up the chain. You have a unique opportunity to invest in employee stocks with an interesting twist!

You're given:

  • n employees, each with ID from 1 to n
  • present[i]: current price to buy employee i's stock today
  • future[i]: expected selling price tomorrow
  • hierarchy: boss-employee relationships
  • budget: total investment funds

The Discount Rule: If a boss buys their own stock, all their direct reports get a 50% discount on their stock price! ๐Ÿ“ˆ

Your goal is to maximize profit by strategically choosing which stocks to buy, considering both the discount opportunities and profit potential. Each stock can only be purchased once, and you must stay within budget.

Example: If Boss A (price $100, profit $20) has Employee B (price $80, profit $50), buying Boss A's stock makes Employee B's stock cost only $40, potentially increasing overall returns!

Input & Output

example_1.py โ€” Basic Company Structure
$ Input: n = 3 present = [100, 80, 60] future = [150, 120, 90] hierarchy = [[1, 2], [1, 3]] budget = 200
โ€บ Output: 90
๐Ÿ’ก Note: Optimal strategy: Buy stocks of employees 1 and 2. Employee 1 costs $100, profit = $50. Employee 2 gets 50% discount (since 1 is bought), costs $40, profit = $40. Total cost = $140, total profit = $90.
example_2.py โ€” Multi-level Hierarchy
$ Input: n = 4 present = [120, 80, 60, 40] future = [150, 100, 90, 70] hierarchy = [[1, 2], [2, 3], [2, 4]] budget = 100
โ€บ Output: 50
๐Ÿ’ก Note: Best strategy: Buy employee 2's stock ($80, profit $20) which gives discount to employees 3 and 4. Then buy employee 4's discounted stock ($20, profit $30). Total cost = $100, total profit = $50.
example_3.py โ€” Limited Budget Edge Case
$ Input: n = 2 present = [100, 200] future = [110, 250] hierarchy = [[1, 2]] budget = 150
โ€บ Output: 10
๐Ÿ’ก Note: With budget $150, we can only afford employee 1's stock ($100, profit $10). Employee 2's stock, even with 50% discount ($100), would exceed our remaining budget of $50.

Visualization

Tap to expand
๐Ÿข Corporate Investment StrategyCEO (Employee 1)$100 โ†’ $150 (Profit: $50)Employee 2$80 โ†’ $120Employee 3$60 โ†’ $90๐Ÿ’ฐ50% Discount!$40 cost๐Ÿ’ฐ50% Discount!$30 cost๐Ÿ’ก Optimal Strategy AnalysisBudget: $200 | Goal: Maximize profit with discount considerationsโœ… Buy CEO stock ($100) โ†’ Unlocks 50% discount for Employees 2 & 3โœ… Buy Employee 2 discounted stock ($40) โ†’ Total cost: $140, Profit: $90๐ŸŽฏ Result: $90 profit (better than buying Employee 3 alone for $30 profit)
Understanding the Visualization
1
Map the Hierarchy
Build the company tree structure showing boss-employee relationships
2
Identify Discount Opportunities
Mark which investments unlock 50% discounts for subordinates
3
Calculate Investment Options
For each employee, compute profit with and without discount
4
Apply Dynamic Programming
Use memoization to find optimal investment combinations
5
Maximize Returns
Select the strategy that yields highest profit within budget
Key Takeaway
๐ŸŽฏ Key Insight: The discount mechanism creates interdependencies between investment choices. Buying a manager's stock can unlock significant savings for their team, making seemingly expensive investments profitable when considering the collective benefit. Dynamic programming efficiently explores these complex decision trees to find the optimal strategy.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n ร— budget)

Each node-budget combination is computed once due to memoization

n
2n
โœ“ Linear Growth
Space Complexity
O(n ร— budget)

Memoization table stores results for all node-budget pairs

n
2n
โšก Linearithmic Space

Constraints

  • 1 โ‰ค n โ‰ค 1000
  • 1 โ‰ค present[i], future[i] โ‰ค 104
  • 1 โ‰ค budget โ‰ค 106
  • hierarchy.length = n - 1
  • Each employee has at most one direct boss
  • The hierarchy forms a valid tree structure
  • Employee 1 is always the root (CEO)
Asked in
Goldman Sachs 45 JPMorgan Chase 38 Microsoft 32 Amazon 28 Google 25
43.6K Views
Medium-High Frequency
~25 min Avg. Time
1.8K 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