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

Commite267f10

Browse files
authored
add js solution for maxProfit with Cooldown
1 parenta9344c2 commite267f10

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

‎problems/0309.最佳买卖股票时机含冷冻期.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,29 @@ class Solution:
207207

208208
Go:
209209

210+
#"diff-d1e1c7e4fc58f53970a9f6b4e35b09b459c925cf7db5c143be2c2a8f33580298-209-211-0" data-selected="false" role="gridcell" tabindex="-1" valign="top">
211+
212+
```javascript
213+
constmaxProfit= (prices)=> {
214+
if(prices.length<2) {
215+
return0
216+
}elseif(prices.length<3) {
217+
returnMath.max(0, prices[1]- prices[0]);
218+
}
219+
220+
let dp=Array.from(Array(prices.length), ()=>Array(4).fill(0));
221+
dp[0][0]=0- prices[0];
222+
223+
for(i=1; i<prices.length; i++) {
224+
dp[i][0]=Math.max(dp[i-1][0],Math.max(dp[i-1][1], dp[i-1][3])- prices[i]);
225+
dp[i][1]=Math.max(dp[i-1][1], dp[i-1][3]);
226+
dp[i][2]= dp[i-1][0]+ prices[i];
227+
dp[i][3]= dp[i-1][2];
228+
}
210229

230+
returnMath.max(dp[prices.length-1][1], dp[prices.length-1][2], dp[prices.length-1][3]);
231+
};
232+
```
211233

212234

213235
-----------------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp