-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbest-time-to-buy-and-sell-stock-iii.cpp
63 lines (54 loc) · 1.91 KB
/
best-time-to-buy-and-sell-stock-iii.cpp
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
#define vi vector<int>
class Solution {
public:
int maxProfit(vector<int> &prices) {
int end = (int) prices.size() - 1;
vi mainProfitInfo = maxProfit(prices, 0, end);
vi mainProfitDiff = minProfit(prices, mainProfitInfo[1], mainProfitInfo[2]);
vi leftProfitInfo = maxProfit(prices, 0, mainProfitInfo[1] - 1);
vi rightProfitInfo = maxProfit(prices, mainProfitInfo[2] + 1, end);
return mainProfitInfo[0] + max(-mainProfitDiff[0], max(leftProfitInfo[0], rightProfitInfo[0]));
}
vi maxProfit(vector<int> &prices, int a, int b) {
if (a >= b){
return {0, a, b};
}
int iMinSoFar = a;
int low = a, high = a;
int minSoFar = numeric_limits<int>::max() >> 1;
int maxDiff = 0;
for (int i = a; i <= b; i++){
if (prices[i] - minSoFar > maxDiff){
maxDiff = prices[i] - minSoFar;
high = i;
low = iMinSoFar;
}
if (prices[i] < minSoFar){
minSoFar = prices[i];
iMinSoFar = i;
}
}
return {maxDiff, low, high};
}
vi minProfit(vector<int> &prices, int a, int b) {
if (a >= b){
return {0, a, b};
}
int iMaxSoFar = a;
int low = a, high = a;
int maxSoFar = numeric_limits<int>::min() >> 1;
int maxDiff = 0;
for (int i = a; i <= b; i++){
if (prices[i] - maxSoFar < maxDiff){
maxDiff = prices[i] - maxSoFar;
high = i;
low = iMaxSoFar;
}
if (prices[i] > maxSoFar){
maxSoFar = prices[i];
iMaxSoFar = i;
}
}
return {maxDiff, low, high};
}
};