File tree Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Expand file tree Collapse file tree 1 file changed +17
-2
lines changed Original file line number Diff line number Diff line change @@ -211,7 +211,7 @@ class Solution { //动态规划
211
211
212
212
213
213
Python:
214
-
214
+ 版本一
215
215
``` python
216
216
class Solution :
217
217
def maxProfit (self , k : int , prices : List[int ]) -> int :
@@ -226,7 +226,22 @@ class Solution:
226
226
dp[i][j+ 2 ] = max (dp[i- 1 ][j+ 2 ], dp[i- 1 ][j+ 1 ] + prices[i])
227
227
return dp[- 1 ][2 * k]
228
228
```
229
-
229
+ 版本二
230
+ ``` python3
231
+ class Solution :
232
+ def maxProfit (self , k : int , prices : List[int ]) -> int :
233
+ if len (prices) == 0 : return 0
234
+ dp = [0 ] * (2 * k + 1 )
235
+ for i in range (1 ,2 * k,2 ):
236
+ dp[i] = - prices[0 ]
237
+ for i in range (1 ,len (prices)):
238
+ for j in range (1 ,2 * k + 1 ):
239
+ if j % 2 :
240
+ dp[j] = max (dp[j],dp[j- 1 ]- prices[i])
241
+ else :
242
+ dp[j] = max (dp[j],dp[j- 1 ]+ prices[i])
243
+ return dp[2 * k]
244
+ ```
230
245
Go:
231
246
232
247
You can’t perform that action at this time.
0 commit comments