3562. Maximum Profit from Trading Stocks with Discounts #2544
-
|
You are given an integer
The company's hierarchy is represented by a 2D integer array Additionally, you have an integer 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 ( Return the maximum profit that can be achieved without exceeding the given budget. Note:
Example 1:
Example 2:
Example 3:
Example 4:
Example 5:
Example 6:
Example 7:
Example 8:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
|
We need to handle a tree structure where the decision to buy at a node affects its children's costs. This is a tree DP problem with knapsack-like constraints. Approach:
Let's implement this solution in PHP: 3562. Maximum Profit from Trading Stocks with Discounts <?php
/**
* @param Integer $n
* @param Integer[] $present
* @param Integer[] $future
* @param Integer[][] $hierarchy
* @param Integer $budget
* @return Integer
*/
function maxProfit($n, $present, $future, $hierarchy, $budget) {
opcache_reset();
set_time_limit(900);
// Build adjacency list for the tree (children of each node)
$children = array_fill(1, $n, []);
foreach ($hierarchy as $edge) {
$children[$edge[0]][] = $edge[1];
}
// Recursive DFS function that returns two DP arrays for a node:
// [dp0, dp1] where:
// dp0[c] = max profit in subtree when parent did NOT buy,
// dp1[c] = max profit in subtree when parent DID buy,
// with total cost <= c.
$dfs = function($u) use (&$dfs, $budget, $present, $future, $children) {
// Leaf node base case
if (empty($children[$u])) {
$cost0 = $present[$u - 1];
$profit0 = $future[$u - 1] - $cost0;
$cost1 = intdiv($present[$u - 1], 2); // floor(present/2)
$profit1 = $future[$u - 1] - $cost1;
$dp0 = array_fill(0, $budget + 1, 0);
$dp1 = array_fill(0, $budget + 1, 0);
// Option to buy (only if cost fits budget)
for ($c = $cost0; $c <= $budget; ++$c) {
$dp0[$c] = max(0, $profit0);
}
for ($c = $cost1; $c <= $budget; ++$c) {
$dp1[$c] = max(0, $profit1);
}
return [$dp0, $dp1];
}
// Merge children's DP arrays for the two cases:
// dp_child0: when current node does NOT buy (children use their dp0)
// dp_child1: when current node buys (children use their dp1)
$dp_child0 = array_fill(0, $budget + 1, 0);
$dp_child1 = array_fill(0, $budget + 1, 0);
foreach ($children[$u] as $v) {
list($dp0_v, $dp1_v) = $dfs($v);
$new_dp0 = array_fill(0, $budget + 1, -PHP_INT_MAX);
$new_dp1 = array_fill(0, $budget + 1, -PHP_INT_MAX);
// Knapsack merge: combine current dp_child with child's dp
for ($i = 0; $i <= $budget; ++$i) {
for ($j = 0; $j <= $budget - $i; ++$j) {
$new_dp0[$i + $j] = max(
$new_dp0[$i + $j],
$dp_child0[$i] + $dp0_v[$j]
);
$new_dp1[$i + $j] = max(
$new_dp1[$i + $j],
$dp_child1[$i] + $dp1_v[$j]
);
}
}
$dp_child0 = $new_dp0;
$dp_child1 = $new_dp1;
}
// Costs and profits for the current node
$cost0 = $present[$u - 1];
$profit0 = $future[$u - 1] - $cost0;
$cost1 = intdiv($present[$u - 1], 2);
$profit1 = $future[$u - 1] - $cost1;
// Initialize with the "do not buy" case (same for dp0 and dp1)
$dp0_u = $dp_child0;
$dp1_u = $dp_child0;
// Consider buying the current node
for ($c = $cost0; $c <= $budget; ++$c) {
$remaining = $c - $cost0;
$dp0_u[$c] = max(
$dp0_u[$c],
$profit0 + $dp_child1[$remaining]
);
}
for ($c = $cost1; $c <= $budget; ++$c) {
$remaining = $c - $cost1;
$dp1_u[$c] = max(
$dp1_u[$c],
$profit1 + $dp_child1[$remaining]
);
}
return [$dp0_u, $dp1_u];
};
// Root (employee 1) has no parent, so use dp0
list($dp0_root, $_) = $dfs(1);
return max($dp0_root);
}
// Test cases
echo shortestSubarrayToRemove([1, 2, 3, 10, 4, 2, 3, 5]) . "\n"; // Output: 3
echo shortestSubarrayToRemove([5, 4, 3, 2, 1]) . "\n"; // Output: 4
echo shortestSubarrayToRemove([1, 2, 3]) . "\n"; // Output: 0
?>Explanation:1. Problem Transformation
2. Dynamic Programming DesignFor each node
3. Recursive Calculation DetailsLeaf Nodes (no children):
Internal Nodes:
4. Time and Space Complexity
5. Key Optimizations
|
Beta Was this translation helpful? Give feedback.



We need to handle a tree structure where the decision to buy at a node affects its children's costs. This is a tree DP problem with knapsack-like constraints.
Approach:
Tree Representation: Build an adjacency list to represent the company hierarchy as a tree.
Dynamic Programming States: For each node
u, compute two DP arrays:dp0[u][c]: Maximum profit inu's subtree when parent did NOT buy, using budgetcdp1[u][c]: Maximum profit inu's subtree when parent DID buy, using budgetcRecursive DFS Calculation: