Skip to content

Commit ce7d600

Browse files
committed
feat: add solutions to lc problem: No.2291
No.2291.Maximum Profit From Trading Stocks
1 parent 1af94b6 commit ce7d600

File tree

7 files changed

+266
-136
lines changed

7 files changed

+266
-136
lines changed

solution/2200-2299/2291.Maximum Profit From Trading Stocks/README.md

Lines changed: 124 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,18 @@
5353

5454
**方法一:动态规划**
5555

56-
$0-1$ 背包问题。
56+
我们定义 $f[i][j]$ 表示前 $i$ 支股票,预算为 $j$ 时的最大收益。那么答案就是 $f[n][budget]$。
57+
58+
对于第 $i$ 支股票,我们有两种选择:
59+
60+
- 不购买,那么 $f[i][j] = f[i - 1][j]$;
61+
- 购买,那么 $f[i][j] = f[i - 1][j - present[i]] + future[i] - present[i]$。
62+
63+
最后返回 $f[n][budget]$ 即可。
64+
65+
时间复杂度 $O(n \times budget)$,空间复杂度 $O(n \times budget)$。其中 $n$ 为数组长度。
66+
67+
我们可以发现,对于每一行,我们只需要用到上一行的值,因此可以将空间复杂度优化到 $O(budget)$。
5768

5869
<!-- tabs:start -->
5970

@@ -64,12 +75,23 @@ $0-1$ 背包问题。
6475
```python
6576
class Solution:
6677
def maximumProfit(self, present: List[int], future: List[int], budget: int) -> int:
67-
arr = [(a, b - a) for a, b in zip(present, future) if b > a]
68-
dp = [0] * (budget + 1)
69-
for v, w in arr:
70-
for j in range(budget, v - 1, -1):
71-
dp[j] = max(dp[j], dp[j - v] + w)
72-
return dp[-1]
78+
f = [[0] * (budget + 1) for _ in range(len(present) + 1)]
79+
for i, w in enumerate(present, 1):
80+
for j in range(budget + 1):
81+
f[i][j] = f[i - 1][j]
82+
if j >= w and future[i - 1] > w:
83+
f[i][j] = max(f[i][j], f[i - 1][j - w] + future[i - 1] - w)
84+
return f[-1][-1]
85+
```
86+
87+
```python
88+
class Solution:
89+
def maximumProfit(self, present: List[int], future: List[int], budget: int) -> int:
90+
f = [0] * (budget + 1)
91+
for a, b in zip(present, future):
92+
for j in range(budget, a - 1, -1):
93+
f[j] = max(f[j], f[j - a] + b - a)
94+
return f[-1]
7395
```
7496

7597
### **Java**
@@ -79,40 +101,34 @@ class Solution:
79101
```java
80102
class Solution {
81103
public int maximumProfit(int[] present, int[] future, int budget) {
82-
List<int[]> arr = new ArrayList<>();
83-
for (int i = 0; i < present.length; ++i) {
84-
if (future[i] > present[i]) {
85-
arr.add(new int[] {present[i], future[i] - present[i]});
86-
}
87-
}
88-
int[] dp = new int[budget + 1];
89-
for (int[] e : arr) {
90-
int v = e[0], w = e[1];
91-
for (int j = budget; j >= v; --j) {
92-
dp[j] = Math.max(dp[j], dp[j - v] + w);
104+
int n = present.length;
105+
int[][] f = new int[n + 1][budget + 1];
106+
for (int i = 1; i <= n; ++i) {
107+
for (int j = 0; j <= budget; ++j) {
108+
f[i][j] = f[i - 1][j];
109+
if (j >= present[i - 1]) {
110+
f[i][j] = Math.max(f[i][j], f[i - 1][j - present[i - 1]] + future[i - 1] - present[i - 1]);
111+
}
93112
}
94113
}
95-
return dp[budget];
114+
return f[n][budget];
96115
}
97116
}
98117
```
99118

100-
### **TypeScript**
101-
102-
```ts
103-
function maximumProfit(
104-
present: number[],
105-
future: number[],
106-
budget: number,
107-
): number {
108-
let packet = present.map((v, i) => [v, future[i] - v]);
109-
let dp = new Array(budget + 1).fill(0);
110-
for (let [v, w] of packet) {
111-
for (let j = budget; j >= v; j--) {
112-
dp[j] = Math.max(dp[j], dp[j - v] + w);
119+
```java
120+
class Solution {
121+
public int maximumProfit(int[] present, int[] future, int budget) {
122+
int n = present.length;
123+
int[] f = new int[budget + 1];
124+
for (int i = 0; i < n; ++i) {
125+
int a = present[i], b = future[i];
126+
for (int j = budget; j >= a; --j) {
127+
f[j] = Math.max(f[j], f[j - a] + b - a);
128+
}
113129
}
130+
return f[budget];
114131
}
115-
return dp[budget];
116132
}
117133
```
118134

@@ -123,13 +139,35 @@ class Solution {
123139
public:
124140
int maximumProfit(vector<int>& present, vector<int>& future, int budget) {
125141
int n = present.size();
126-
vector<int> dp(budget + 1);
127-
for (int i = 0; i < n; i++) {
128-
for (int j = budget; j >= present[i]; j--) {
129-
dp[j] = max(dp[j], dp[j - present[i]] + future[i] - present[i]);
142+
int f[n + 1][budget + 1];
143+
memset(f, 0, sizeof f);
144+
for (int i = 1; i <= n; ++i) {
145+
for (int j = 0; j <= budget; ++j) {
146+
f[i][j] = f[i - 1][j];
147+
if (j >= present[i - 1]) {
148+
f[i][j] = max(f[i][j], f[i - 1][j - present[i - 1]] + future[i - 1] - present[i - 1]);
149+
}
130150
}
131151
}
132-
return dp.back();
152+
return f[n][budget];
153+
}
154+
};
155+
```
156+
157+
```cpp
158+
class Solution {
159+
public:
160+
int maximumProfit(vector<int>& present, vector<int>& future, int budget) {
161+
int n = present.size();
162+
int f[budget + 1];
163+
memset(f, 0, sizeof f);
164+
for (int i = 0; i < n; ++i) {
165+
int a = present[i], b = future[i];
166+
for (int j = budget; j >= a; --j) {
167+
f[j] = max(f[j], f[j - a] + b - a);
168+
}
169+
}
170+
return f[budget];
133171
}
134172
};
135173
```
@@ -138,20 +176,39 @@ public:
138176

139177
```go
140178
func maximumProfit(present []int, future []int, budget int) int {
141-
arr := [][]int{}
142-
for i, v := range present {
143-
if future[i] > v {
144-
arr = append(arr, []int{v, future[i] - v})
179+
n := len(present)
180+
f := make([][]int, n+1)
181+
for i := range f {
182+
f[i] = make([]int, budget+1)
183+
}
184+
for i := 1; i <= n; i++ {
185+
for j := 0; j <= budget; j++ {
186+
f[i][j] = f[i-1][j]
187+
if j >= present[i-1] {
188+
f[i][j] = max(f[i][j], f[i-1][j-present[i-1]]+future[i-1]-present[i-1])
189+
}
145190
}
146191
}
147-
dp := make([]int, budget+1)
148-
for _, e := range arr {
149-
v, w := e[0], e[1]
150-
for j := budget; j >= v; j-- {
151-
dp[j] = max(dp[j], dp[j-v]+w)
192+
return f[n][budget]
193+
}
194+
195+
func max(a, b int) int {
196+
if a > b {
197+
return a
198+
}
199+
return b
200+
}
201+
```
202+
203+
```go
204+
func maximumProfit(present []int, future []int, budget int) int {
205+
f := make([]int, budget+1)
206+
for i, a := range present {
207+
for j := budget; j >= a; j-- {
208+
f[j] = max(f[j], f[j-a]+future[i]-a)
152209
}
153210
}
154-
return dp[budget]
211+
return f[budget]
155212
}
156213

157214
func max(a, b int) int {
@@ -162,6 +219,25 @@ func max(a, b int) int {
162219
}
163220
```
164221

222+
### **TypeScript**
223+
224+
```ts
225+
function maximumProfit(
226+
present: number[],
227+
future: number[],
228+
budget: number,
229+
): number {
230+
const f = new Array(budget + 1).fill(0);
231+
for (let i = 0; i < present.length; ++i) {
232+
const [a, b] = [present[i], future[i]];
233+
for (let j = budget; j >= a; --j) {
234+
f[j] = Math.max(f[j], f[j - a] + b - a);
235+
}
236+
}
237+
return f[budget];
238+
}
239+
```
240+
165241
### **...**
166242

167243
```

0 commit comments

Comments
 (0)