-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbestTimeToBuyAndSellStockIii.js
72 lines (61 loc) · 2.27 KB
/
bestTimeToBuyAndSellStockIii.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Source : https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iii/
// Author : Wei XIE
// Date : 2021-05-05
/**********************************************************************************
Solution 1
Runtime: 824 ms, faster than 11.56% of JavaScript online submissions for Best Time to Buy and Sell Stock III.
Memory Usage: 84.2 MB, less than 6.67% of JavaScript online submissions for Best Time to Buy and Sell Stock III.
**********************************************************************************/
/**
* @param {number[]} prices
* @return {number}
*/
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let n = prices.length;
let max_k = 2;
let dp = Array.from(new Array(n), () => new Array(max_k+1));
for(let i = 0;i < n;i++){
for(let j = 0; j <= max_k; j++){
dp[i][j] = new Array(2).fill(0);
}
}
for (let i = 0; i < n; i++) {
for (let k = max_k; k >= 1; k--) {
if (i == 0) {
dp[0][k][1] = -prices[i];
dp[0][k][0] = 0;
continue;
}
dp[i][k][0] = Math.max(dp[i-1][k][0], dp[i-1][k][1] + prices[i]);
dp[i][k][1] = Math.max(dp[i-1][k][1], dp[i-1][k-1][0] - prices[i]);
}
}
return dp[n - 1][max_k][0];
};
/**********************************************************************************
Solution 2
Runtime: 96 ms, faster than 95.98% of JavaScript online submissions for Best Time to Buy and Sell Stock III.
Memory Usage: 49.6 MB, less than 79.46% of JavaScript online submissions for Best Time to Buy and Sell Stock III.
**********************************************************************************/
/**
* @param {number[]} prices
* @return {number}
*/
var maxProfit = function(prices) {
let n = prices.length;
let dp_i_1_0 = 0;
let dp_i_1_1 = - prices[0];
let dp_i_2_0 = 0;
let dp_i_2_1 = - prices[0];
for(let i = 0;i < n;i++){
dp_i_1_0 = Math.max(dp_i_1_0, dp_i_1_1 + prices[i]);
dp_i_1_1 = Math.max(dp_i_1_1, - prices[i]);
dp_i_2_0 = Math.max(dp_i_2_0, dp_i_2_1 + prices[i]);
dp_i_2_1 = Math.max(dp_i_2_1, dp_i_1_0 - prices[i]);
}
return dp_i_2_0;
};