File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -258,7 +258,30 @@ class Solution:
258
258
Go:
259
259
260
260
261
+ Javascript:
261
262
263
+ ``` javascript
264
+ const maxProfit = (k ,prices ) => {
265
+ if (prices == null || prices .length < 2 || k == 0 ) {
266
+ return 0 ;
267
+ }
268
+
269
+ let dp = Array .from (Array (prices .length ), () => Array (2 * k+ 1 ).fill (0 ));
270
+
271
+ for (let j = 1 ; j < 2 * k; j += 2 ) {
272
+ dp[0 ][j] = 0 - prices[0 ];
273
+ }
274
+
275
+ for (let i = 1 ; i < prices .length ; i++ ) {
276
+ for (let j = 0 ; j < 2 * k; j += 2 ) {
277
+ dp[i][j+ 1 ] = Math .max (dp[i- 1 ][j+ 1 ], dp[i- 1 ][j] - prices[i]);
278
+ dp[i][j+ 2 ] = Math .max (dp[i- 1 ][j+ 2 ], dp[i- 1 ][j+ 1 ] + prices[i]);
279
+ }
280
+ }
281
+
282
+ return dp[prices .length - 1 ][2 * k];
283
+ };
284
+ ```
262
285
263
286
-----------------------
264
287
* 作者微信:[ 程序员Carl] ( https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw )
You can’t perform that action at this time.
0 commit comments