|
| 1 | +<h2><a href="https://leetcode.com/problems/maximum-profit-from-trading-stocks-with-discounts">3854. Maximum Profit from Trading Stocks with Discounts</a></h2><h3>Hard</h3><hr><p>You are given an integer <code>n</code>, representing the number of employees in a company. Each employee is assigned a unique ID from 1 to <code>n</code>, and employee 1 is the CEO. You are given two <strong>1-based </strong>integer arrays, <code>present</code> and <code>future</code>, each of length <code>n</code>, where:</p> |
| 2 | + |
| 3 | +<ul> |
| 4 | + <li><code>present[i]</code> represents the <strong>current</strong> price at which the <code>i<sup>th</sup></code> employee can buy a stock today.</li> |
| 5 | + <li><code>future[i]</code> represents the <strong>expected</strong> price at which the <code>i<sup>th</sup></code> employee can sell the stock tomorrow.</li> |
| 6 | +</ul> |
| 7 | + |
| 8 | +<p>The company's hierarchy is represented by a 2D integer array <code>hierarchy</code>, where <code>hierarchy[i] = [u<sub>i</sub>, v<sub>i</sub>]</code> means that employee <code>u<sub>i</sub></code> is the direct boss of employee <code>v<sub>i</sub></code>.</p> |
| 9 | + |
| 10 | +<p>Additionally, you have an integer <code>budget</code> representing the total funds available for investment.</p> |
| 11 | + |
| 12 | +<p>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 <strong>half</strong> the original price (<code>floor(present[v] / 2)</code>).</p> |
| 13 | + |
| 14 | +<p>Return the <strong>maximum</strong> profit that can be achieved without exceeding the given budget.</p> |
| 15 | + |
| 16 | +<p><strong>Note:</strong></p> |
| 17 | + |
| 18 | +<ul> |
| 19 | + <li>You may buy each stock at most <strong>once</strong>.</li> |
| 20 | + <li>You <strong>cannot</strong> use any profit earned from future stock prices to fund additional investments and must buy only from <code>budget</code>.</li> |
| 21 | +</ul> |
| 22 | + |
| 23 | +<p> </p> |
| 24 | +<p><strong class="example">Example 1:</strong></p> |
| 25 | + |
| 26 | +<div class="example-block"> |
| 27 | +<p><strong>Input:</strong> <span class="example-io">n = 2, present = [1,2], future = [4,3], hierarchy = [[1,2]], budget = 3</span></p> |
| 28 | + |
| 29 | +<p><strong>Output:</strong> <span class="example-io">5</span></p> |
| 30 | + |
| 31 | +<p><strong>Explanation:</strong></p> |
| 32 | + |
| 33 | +<p><img src="https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-053641.png" style="width: 200px; height: 80px;" /></p> |
| 34 | + |
| 35 | +<ul> |
| 36 | + <li>Employee 1 buys the stock at price 1 and earns a profit of <code>4 - 1 = 3</code>.</li> |
| 37 | + <li>Since Employee 1 is the direct boss of Employee 2, Employee 2 gets a discounted price of <code>floor(2 / 2) = 1</code>.</li> |
| 38 | + <li>Employee 2 buys the stock at price 1 and earns a profit of <code>3 - 1 = 2</code>.</li> |
| 39 | + <li>The total buying cost is <code>1 + 1 = 2 <= budget</code>. Thus, the maximum total profit achieved is <code>3 + 2 = 5</code>.</li> |
| 40 | +</ul> |
| 41 | +</div> |
| 42 | + |
| 43 | +<p><strong class="example">Example 2:</strong></p> |
| 44 | + |
| 45 | +<div class="example-block"> |
| 46 | +<p><strong>Input:</strong> <span class="example-io">n = 2, present = [3,4], future = [5,8], hierarchy = [[1,2]], budget = 4</span></p> |
| 47 | + |
| 48 | +<p><strong>Output:</strong> <span class="example-io">4</span></p> |
| 49 | + |
| 50 | +<p><strong>Explanation:</strong></p> |
| 51 | + |
| 52 | +<p><img src="https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-053641.png" style="width: 200px; height: 80px;" /></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li>Employee 2 buys the stock at price 4 and earns a profit of <code>8 - 4 = 4</code>.</li> |
| 56 | + <li>Since both employees cannot buy together, the maximum profit is 4.</li> |
| 57 | +</ul> |
| 58 | +</div> |
| 59 | + |
| 60 | +<p><strong class="example">Example 3:</strong></p> |
| 61 | + |
| 62 | +<div class="example-block"> |
| 63 | +<p><strong>Input:</strong> <span class="example-io">n = 3, present = [4,6,8], future = [7,9,11], hierarchy = [[1,2],[1,3]], budget = 10</span></p> |
| 64 | + |
| 65 | +<p><strong>Output:</strong> 10</p> |
| 66 | + |
| 67 | +<p><strong>Explanation:</strong></p> |
| 68 | + |
| 69 | +<p><img src="https://assets.leetcode.com/uploads/2025/04/09/image.png" style="width: 180px; height: 153px;" /></p> |
| 70 | + |
| 71 | +<ul> |
| 72 | + <li>Employee 1 buys the stock at price 4 and earns a profit of <code>7 - 4 = 3</code>.</li> |
| 73 | + <li>Employee 3 would get a discounted price of <code>floor(8 / 2) = 4</code> and earns a profit of <code>11 - 4 = 7</code>.</li> |
| 74 | + <li>Employee 1 and Employee 3 buy their stocks at a total cost of <code>4 + 4 = 8 <= budget</code>. Thus, the maximum total profit achieved is <code>3 + 7 = 10</code>.</li> |
| 75 | +</ul> |
| 76 | +</div> |
| 77 | + |
| 78 | +<p><strong class="example">Example 4:</strong></p> |
| 79 | + |
| 80 | +<div class="example-block"> |
| 81 | +<p><strong>Input:</strong> <span class="example-io">n = 3, present = [5,2,3], future = [8,5,6], hierarchy = [[1,2],[2,3]], budget = 7</span></p> |
| 82 | + |
| 83 | +<p><strong>Output:</strong> <span class="example-io">12</span></p> |
| 84 | + |
| 85 | +<p><strong>Explanation:</strong></p> |
| 86 | + |
| 87 | +<p><img src="https://assets.leetcode.com/uploads/2025/04/09/screenshot-2025-04-10-at-054114.png" style="width: 300px; height: 85px;" /></p> |
| 88 | + |
| 89 | +<ul> |
| 90 | + <li>Employee 1 buys the stock at price 5 and earns a profit of <code>8 - 5 = 3</code>.</li> |
| 91 | + <li>Employee 2 would get a discounted price of <code>floor(2 / 2) = 1</code> and earns a profit of <code>5 - 1 = 4</code>.</li> |
| 92 | + <li>Employee 3 would get a discounted price of <code>floor(3 / 2) = 1</code> and earns a profit of <code>6 - 1 = 5</code>.</li> |
| 93 | + <li>The total cost becomes <code>5 + 1 + 1 = 7 <= budget</code>. Thus, the maximum total profit achieved is <code>3 + 4 + 5 = 12</code>.</li> |
| 94 | +</ul> |
| 95 | +</div> |
| 96 | + |
| 97 | +<p> </p> |
| 98 | +<p><strong>Constraints:</strong></p> |
| 99 | + |
| 100 | +<ul> |
| 101 | + <li><code>1 <= n <= 160</code></li> |
| 102 | + <li><code>present.length, future.length == n</code></li> |
| 103 | + <li><code>1 <= present[i], future[i] <= 50</code></li> |
| 104 | + <li><code>hierarchy.length == n - 1</code></li> |
| 105 | + <li><code>hierarchy[i] == [u<sub>i</sub>, v<sub>i</sub>]</code></li> |
| 106 | + <li><code>1 <= u<sub>i</sub>, v<sub>i</sub> <= n</code></li> |
| 107 | + <li><code>u<sub>i</sub> != v<sub>i</sub></code></li> |
| 108 | + <li><code>1 <= budget <= 160</code></li> |
| 109 | + <li>There are no duplicate edges.</li> |
| 110 | + <li>Employee 1 is the direct or indirect boss of every employee.</li> |
| 111 | + <li>The input graph <code>hierarchy </code>is <strong>guaranteed</strong> to have no cycles.</li> |
| 112 | +</ul> |
0 commit comments