Skip to content

Commit 8a18ddd

Browse files
chore: add LeetCode daily solution
1 parent 6bfddec commit 8a18ddd

File tree

5 files changed

+156
-0
lines changed

5 files changed

+156
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Maximum Profit from Trading Stocks with Discounts (Hard)
2+
3+
**Problem ID:** 3562
4+
**Date:** 2025-12-16
5+
**Link:** https://leetcode.com/problems/maximum-profit-from-trading-stocks-with-discounts/
6+
7+
## Approach
8+
9+
To solve the problem of maximizing profit from stock trading with discounts in a company hierarchy, we can employ a dynamic programming approach combined with a depth-first search (DFS) strategy.
10+
11+
### Main Idea:
12+
The goal is to determine the maximum profit achievable by selecting which employees can buy stocks while respecting the budget constraint. Each employee can either buy their stock at the regular price or, if their direct boss buys their stock, at a discounted price. The challenge lies in optimizing the selection of employees to maximize profit without exceeding the budget.
13+
14+
### Approach:
15+
1. **Graph Representation**: First, represent the hierarchy using an adjacency list. This allows us to easily traverse the relationships between employees.
16+
17+
2. **Profit Calculation**: For each employee, calculate the profit they can make if they buy their stock at the regular price and at the discounted price (if applicable). Store these profits in a list.
18+
19+
3. **Dynamic Programming Setup**: Use a dynamic programming array `dp` where `dp[b]` represents the maximum profit achievable with a budget of `b`. The size of this array will be `budget + 1`.
20+
21+
4. **DFS for Employee Selection**: Implement a DFS function that explores all possible combinations of employees buying stocks. For each employee, consider:
22+
- Buying at the regular price.
23+
- Buying at the discounted price (if their boss has bought).
24+
- Not buying at all.
25+
For each choice, update the `dp` array accordingly.
26+
27+
5. **Budget Management**: Ensure that when an employee's stock is bought, the budget is updated, and only valid budget states are considered.
28+
29+
6. **Result Extraction**: After processing all employees, the maximum value in the `dp` array will represent the maximum profit achievable within the given budget.
30+
31+
### Data Structures:
32+
- **Adjacency List**: To represent the hierarchy of employees.
33+
- **Dynamic Programming Array**: To track the maximum profit for each budget state.
34+
35+
### Complexity:
36+
- **Time Complexity**: The overall complexity is O(n * budget), where `n` is the number of employees and `budget` is the maximum budget allowed. The DFS may explore multiple combinations, but due to the constraints (with `n` and `budget` both ≤ 160), this is manageable.
37+
- **Space Complexity**: O(n + budget), accounting for the adjacency list and the dynamic programming array.
38+
39+
By following this structured approach, we can efficiently determine the maximum profit achievable from stock trading within the constraints provided.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public int maximumProfit(int n, int[] present, int[] future, int[][] hierarchy, int budget) {
5+
List<List<Integer>> graph = new ArrayList<>();
6+
for (int i = 0; i <= n; i++) {
7+
graph.add(new ArrayList<>());
8+
}
9+
for (int[] edge : hierarchy) {
10+
graph.get(edge[0]).add(edge[1]);
11+
}
12+
13+
int[] profits = new int[n + 1];
14+
int[] costs = new int[n + 1];
15+
16+
for (int i = 1; i <= n; i++) {
17+
profits[i] = future[i - 1] - present[i - 1];
18+
costs[i] = present[i - 1];
19+
}
20+
21+
for (int i = 1; i <= n; i++) {
22+
for (int child : graph.get(i)) {
23+
costs[child] = Math.min(costs[child], present[child - 1] / 2);
24+
}
25+
}
26+
27+
int[][] dp = new int[n + 1][budget + 1];
28+
29+
for (int i = 1; i <= n; i++) {
30+
for (int j = 0; j <= budget; j++) {
31+
dp[i][j] = dp[i - 1][j];
32+
if (j >= costs[i]) {
33+
dp[i][j] = Math.max(dp[i][j], dp[i - 1][j - costs[i]] + profits[i]);
34+
}
35+
}
36+
}
37+
38+
return dp[n][budget];
39+
}
40+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Solution {
2+
maxProfit(n, present, future, hierarchy, budget) {
3+
const graph = Array.from({ length: n + 1 }, () => []);
4+
for (const [u, v] of hierarchy) {
5+
graph[u].push(v);
6+
}
7+
8+
const dp = Array.from({ length: budget + 1 }, () => 0);
9+
10+
const dfs = (employee) => {
11+
const costs = [];
12+
const profits = [];
13+
costs.push(present[employee - 1]);
14+
profits.push(future[employee - 1] - present[employee - 1]);
15+
16+
for (const subordinate of graph[employee]) {
17+
const [subCost, subProfit] = dfs(subordinate);
18+
costs.push(Math.floor(present[subordinate - 1] / 2));
19+
profits.push(future[subordinate - 1] - Math.floor(present[subordinate - 1] / 2));
20+
}
21+
22+
const newDp = [...dp];
23+
for (let i = budget; i >= 0; i--) {
24+
for (let j = 0; j < costs.length; j++) {
25+
if (i >= costs[j]) {
26+
newDp[i] = Math.max(newDp[i], dp[i - costs[j]] + profits[j]);
27+
}
28+
}
29+
}
30+
return [costs, profits];
31+
};
32+
33+
dfs(1);
34+
return Math.max(...dp);
35+
}
36+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution:
2+
def maximumProfit(self, n: int, present: List[int], future: List[int], hierarchy: List[List[int]], budget: int) -> int:
3+
from collections import defaultdict
4+
import heapq
5+
6+
graph = defaultdict(list)
7+
for u, v in hierarchy:
8+
graph[u].append(v)
9+
10+
profits = []
11+
costs = []
12+
13+
def dfs(employee):
14+
nonlocal profits, costs
15+
buy_cost = present[employee - 1]
16+
sell_price = future[employee - 1]
17+
profit = sell_price - buy_cost
18+
19+
profits.append(profit)
20+
costs.append(buy_cost)
21+
22+
for subordinate in graph[employee]:
23+
discounted_cost = present[subordinate - 1] // 2
24+
discounted_profit = future[subordinate - 1] - discounted_cost
25+
26+
profits.append(discounted_profit)
27+
costs.append(discounted_cost)
28+
29+
dfs(subordinate)
30+
31+
dfs(1)
32+
33+
max_profit = 0
34+
dp = [0] * (budget + 1)
35+
36+
for cost, profit in zip(costs, profits):
37+
for b in range(budget, cost - 1, -1):
38+
dp[b] = max(dp[b], dp[b - cost] + profit)
39+
40+
return max(dp)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,4 @@ Through completing the Blind 75 and NeetCode 150, you will have mastered:
306306
- 2025-12-13 — [Coupon Code Validator](https://leetcode.com/problems/coupon-code-validator/) (Easy) → `Easy/2025-12-13-3606-Coupon-Code-Validator`
307307
- 2025-12-14 — [Number of Ways to Divide a Long Corridor](https://leetcode.com/problems/number-of-ways-to-divide-a-long-corridor/) (Hard) → `Hard/2025-12-14-2147-Number-of-Ways-to-Divide-a-Long-Corridor`
308308
- 2025-12-15 — [Number of Smooth Descent Periods of a Stock](https://leetcode.com/problems/number-of-smooth-descent-periods-of-a-stock/) (Medium) → `Medium/2025-12-15-2110-Number-of-Smooth-Descent-Periods-of-a-Stock`
309+
- 2025-12-16 — [Maximum Profit from Trading Stocks with Discounts](https://leetcode.com/problems/maximum-profit-from-trading-stocks-with-discounts/) (Hard) → `Hard/2025-12-16-3562-Maximum-Profit-from-Trading-Stocks-with-Discounts`

0 commit comments

Comments
 (0)