Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb64df0f

Browse files
Merge pull requestyoungyangyang04#485 from jackeyjia/patch-9
add js solution for maxProfit IV
2 parents3d41c31 +486fff2 commitb64df0f

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎problems/0188.买卖股票的最佳时机IV.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,30 @@ class Solution:
258258
Go:
259259

260260

261+
Javascript:
261262

263+
```javascript
264+
constmaxProfit= (k,prices)=> {
265+
if (prices==null||prices.length<2|| k==0) {
266+
return0;
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+
```
262285

263286
-----------------------
264287
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp