-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
33 lines (31 loc) · 916 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
*
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
if (prices.length <= 1) {
return 0;
}
let dp1 = Math.max(-prices[0], -prices[1]);
let dp2 = prices[1] - prices[0];
let dp3 = Math.max(0, dp2);
for (let i = 2; i < prices.length; i++) {
// 只需要记录上一次的状态,可以不用存储每一次的状态
const last_dp3 = dp3;
const last_dp2 = dp2;
dp2 = dp1 + prices[i];
dp3 = Math.max(last_dp2, dp3);
dp1 = Math.max(dp1, last_dp3 - prices[i]);
}
return Math.max(dp3, dp2);
};
console.log(maxProfit([1,2,3,0,2]));
console.log(maxProfit([1,2,3,4]));
module.exports = {
id:'309',
title:'Best Time to Buy and Sell Stock with Cooldown',
url:'https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/',
difficulty:'medium',
have_md:true,
};