diff --git a/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README.md b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README.md index 56c0d505291b9..f3b4979d3493b 100644 --- a/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README.md +++ b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README.md @@ -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[] 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& present, vector& future, vector>& hierarchy, int budget) { + vector> g(n + 1); + for (auto& e : hierarchy) { + g[e[0]].push_back(e[1]); + } + + auto dfs = [&](const auto& dfs, int u) -> vector> { + vector> 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> 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]; +} ``` diff --git a/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README_EN.md b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README_EN.md index 2f6564a3fcc00..4c2c2f2eae378 100644 --- a/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README_EN.md +++ b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/README_EN.md @@ -147,25 +147,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[] 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& present, vector& future, vector>& hierarchy, int budget) { + vector> g(n + 1); + for (auto& e : hierarchy) { + g[e[0]].push_back(e[1]); + } + + auto dfs = [&](const auto& dfs, int u) -> vector> { + vector> 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> 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]; +} ``` diff --git a/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.cpp b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.cpp new file mode 100644 index 0000000000000..27fb163d40de8 --- /dev/null +++ b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + int maxProfit(int n, vector& present, vector& future, vector>& hierarchy, int budget) { + vector> g(n + 1); + for (auto& e : hierarchy) { + g[e[0]].push_back(e[1]); + } + + auto dfs = [&](const auto& dfs, int u) -> vector> { + vector> 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> 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]; + } +}; diff --git a/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.go b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.go new file mode 100644 index 0000000000000..400325111a657 --- /dev/null +++ b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.go @@ -0,0 +1,41 @@ +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] +} diff --git a/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.java b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.java new file mode 100644 index 0000000000000..9eb3c46dd754f --- /dev/null +++ b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.java @@ -0,0 +1,55 @@ +class Solution { + private List[] 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; + } +} diff --git a/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.py b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.py new file mode 100644 index 0000000000000..e9b50610ec62f --- /dev/null +++ b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.py @@ -0,0 +1,39 @@ +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] diff --git a/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.ts b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.ts new file mode 100644 index 0000000000000..e398afcea238d --- /dev/null +++ b/solution/3500-3599/3562.Maximum Profit from Trading Stocks with Discounts/Solution.ts @@ -0,0 +1,47 @@ +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]; +}