Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -150,25 +150,254 @@ tags:
#### Python3

```python

class Solution:
def maxProfit(
self,
n: int,
present: List[int],
future: List[int],
hierarchy: List[List[int]],
budget: int,
) -> int:
max = lambda a, b: a if a > b else b
g = [[] for _ in range(n + 1)]
for u, v in hierarchy:
g[u].append(v)

def dfs(u: int):
nxt = [[0, 0] for _ in range(budget + 1)]
for v in g[u]:
fv = dfs(v)
for j in range(budget, -1, -1):
for jv in range(j + 1):
for pre in (0, 1):
val = nxt[j - jv][pre] + fv[jv][pre]
if val > nxt[j][pre]:
nxt[j][pre] = val

f = [[0, 0] for _ in range(budget + 1)]
price = future[u - 1]

for j in range(budget + 1):
for pre in (0, 1):
cost = present[u - 1] // (pre + 1)
if j >= cost:
f[j][pre] = max(nxt[j][0], nxt[j - cost][1] + (price - cost))
else:
f[j][pre] = nxt[j][0]

return f

return dfs(1)[budget][0]
```

#### Java

```java

class Solution {
private List<Integer>[] g;
private int[] present;
private int[] future;
private int budget;

public int maxProfit(int n, int[] present, int[] future, int[][] hierarchy, int budget) {
this.present = present;
this.future = future;
this.budget = budget;

g = new ArrayList[n + 1];
Arrays.setAll(g, k -> new ArrayList<>());

for (int[] e : hierarchy) {
g[e[0]].add(e[1]);
}

return dfs(1)[budget][0];
}

private int[][] dfs(int u) {
int[][] nxt = new int[budget + 1][2];

for (int v : g[u]) {
int[][] fv = dfs(v);
for (int j = budget; j >= 0; j--) {
for (int jv = 0; jv <= j; jv++) {
for (int pre = 0; pre < 2; pre++) {
int val = nxt[j - jv][pre] + fv[jv][pre];
if (val > nxt[j][pre]) {
nxt[j][pre] = val;
}
}
}
}
}

int[][] f = new int[budget + 1][2];
int price = future[u - 1];

for (int j = 0; j <= budget; j++) {
for (int pre = 0; pre < 2; pre++) {
int cost = present[u - 1] / (pre + 1);
if (j >= cost) {
f[j][pre] = Math.max(nxt[j][0], nxt[j - cost][1] + (price - cost));
} else {
f[j][pre] = nxt[j][0];
}
}
}

return f;
}
}
```

#### C++

```cpp

class Solution {
public:
int maxProfit(int n, vector<int>& present, vector<int>& future, vector<vector<int>>& hierarchy, int budget) {
vector<vector<int>> g(n + 1);
for (auto& e : hierarchy) {
g[e[0]].push_back(e[1]);
}

auto dfs = [&](const auto& dfs, int u) -> vector<array<int, 2>> {
vector<array<int, 2>> nxt(budget + 1);
for (int j = 0; j <= budget; j++) nxt[j] = {0, 0};

for (int v : g[u]) {
auto fv = dfs(dfs, v);
for (int j = budget; j >= 0; j--) {
for (int jv = 0; jv <= j; jv++) {
for (int pre = 0; pre < 2; pre++) {
int val = nxt[j - jv][pre] + fv[jv][pre];
if (val > nxt[j][pre]) {
nxt[j][pre] = val;
}
}
}
}
}

vector<array<int, 2>> f(budget + 1);
int price = future[u - 1];

for (int j = 0; j <= budget; j++) {
for (int pre = 0; pre < 2; pre++) {
int cost = present[u - 1] / (pre + 1);
if (j >= cost) {
f[j][pre] = max(nxt[j][0], nxt[j - cost][1] + (price - cost));
} else {
f[j][pre] = nxt[j][0];
}
}
}

return f;
};

return dfs(dfs, 1)[budget][0];
}
};
```

#### Go

```go
func maxProfit(n int, present []int, future []int, hierarchy [][]int, budget int) int {
g := make([][]int, n+1)
for _, e := range hierarchy {
u, v := e[0], e[1]
g[u] = append(g[u], v)
}

var dfs func(u int) [][2]int
dfs = func(u int) [][2]int {
nxt := make([][2]int, budget+1)

for _, v := range g[u] {
fv := dfs(v)
for j := budget; j >= 0; j-- {
for jv := 0; jv <= j; jv++ {
for pre := 0; pre < 2; pre++ {
nxt[j][pre] = max(nxt[j][pre], nxt[j-jv][pre]+fv[jv][pre])
}
}
}
}

f := make([][2]int, budget+1)
price := future[u-1]

for j := 0; j <= budget; j++ {
for pre := 0; pre < 2; pre++ {
cost := present[u-1] / (pre + 1)
if j >= cost {
buyProfit := nxt[j-cost][1] + (price - cost)
f[j][pre] = max(nxt[j][0], buyProfit)
} else {
f[j][pre] = nxt[j][0]
}
}
}
return f
}

return dfs(1)[budget][0]
}
```

#### TypeScript

```ts
function maxProfit(
n: number,
present: number[],
future: number[],
hierarchy: number[][],
budget: number,
): number {
const g: number[][] = Array.from({ length: n + 1 }, () => []);

for (const [u, v] of hierarchy) {
g[u].push(v);
}

const dfs = (u: number): number[][] => {
const nxt: number[][] = Array.from({ length: budget + 1 }, () => [0, 0]);

for (const v of g[u]) {
const fv = dfs(v);
for (let j = budget; j >= 0; j--) {
for (let jv = 0; jv <= j; jv++) {
for (let pre = 0; pre < 2; pre++) {
nxt[j][pre] = Math.max(nxt[j][pre], nxt[j - jv][pre] + fv[jv][pre]);
}
}
}
}

const f: number[][] = Array.from({ length: budget + 1 }, () => [0, 0]);
const price = future[u - 1];

for (let j = 0; j <= budget; j++) {
for (let pre = 0; pre < 2; pre++) {
const cost = Math.floor(present[u - 1] / (pre + 1));
if (j >= cost) {
const profitIfBuy = nxt[j - cost][1] + (price - cost);
f[j][pre] = Math.max(nxt[j][0], profitIfBuy);
} else {
f[j][pre] = nxt[j][0];
}
}
}

return f;
};

return dfs(1)[budget][0];
}
```

<!-- tabs:end -->
Expand Down
Loading