-
Notifications
You must be signed in to change notification settings - Fork 130
/
Best Time to Buy and Sell Stock III.js
49 lines (40 loc) · 1.23 KB
/
Best Time to Buy and Sell Stock III.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
/**
Say you have an array for which the ith element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete at most two transactions.
Note:
You may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
*/
/**
* @param {number[]} prices
* @return {number}
*
* 数组l[i]记录了price[0..i]的最大profit,
* 数组r[i]记录了price[i..n]的最大profit。
* 已知l[i],求l[i+1]是简单的,同样已知r[i],求r[i-1]也很容易。
* 最后,我们再用O(n)的时间找出最大的l[i]+r[i],即为题目所求。
*/
var maxProfit = function(prices) {
var len = prices.length,
l = [],
r = [],
i,
max,
min;
l[0] = 0;
min = prices[0];
for (i = 1; i < len; i++) {
l[i] = Math.max(l[0], prices[i] - min);
min = Math.min(min, prices[i]);
}
r[len - 1] = 0;
max = prices[len - 1];
for (i = len - 2; i >= 0; i--) {
r[i] = Math.max(r[i + 1], max - prices[i]);
max = Math.max(max, prices[i]);
}
max = 0;
for (i = 0; i < len; i++) {
max = Math.max(max, l[i] + r[i]);
}
return max;
};