53
53
54
54
** 方法一:动态规划**
55
55
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)$。
57
68
58
69
<!-- tabs:start -->
59
70
@@ -64,12 +75,23 @@ $0-1$ 背包问题。
64
75
``` python
65
76
class Solution :
66
77
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 ]
73
95
```
74
96
75
97
### ** Java**
@@ -79,40 +101,34 @@ class Solution:
79
101
``` java
80
102
class Solution {
81
103
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
+ }
93
112
}
94
113
}
95
- return dp [budget];
114
+ return f[n] [budget];
96
115
}
97
116
}
98
117
```
99
118
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
+ }
113
129
}
130
+ return f[budget];
114
131
}
115
- return dp [budget ];
116
132
}
117
133
```
118
134
@@ -123,13 +139,35 @@ class Solution {
123
139
public:
124
140
int maximumProfit(vector<int >& present, vector<int >& future, int budget) {
125
141
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
+ }
130
150
}
131
151
}
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];
133
171
}
134
172
};
135
173
```
@@ -138,20 +176,39 @@ public:
138
176
139
177
``` go
140
178
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
+ }
145
190
}
146
191
}
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)
152
209
}
153
210
}
154
- return dp [budget]
211
+ return f [budget]
155
212
}
156
213
157
214
func max (a , b int ) int {
@@ -162,6 +219,25 @@ func max(a, b int) int {
162
219
}
163
220
```
164
221
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
+
165
241
### ** ...**
166
242
167
243
```
0 commit comments