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

Commit362a130

Browse files
committed
update: DP method
1 parentb29e9a6 commit362a130

File tree

1 file changed

+18
-1
lines changed
  • src/best-time-to-buy-and-sell-stock

1 file changed

+18
-1
lines changed

‎src/best-time-to-buy-and-sell-stock/res.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,21 @@ var maxProfit = function(prices) {
2222
}
2323

2424
returnres;
25-
};
25+
};
26+
27+
/**
28+
* DP 解法
29+
*@param {*} prices
30+
*/
31+
varmaxProfit_2=(prices)=>{
32+
if(prices.length<2)return0;
33+
34+
letlast=0,max=0;
35+
36+
for(leti=1;i<prices.length;i++){
37+
last=Math.max(0,last+prices[i]-prices[i-1]);
38+
max=Math.max(last,max);
39+
}
40+
41+
returnmax;
42+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp