-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path123_BestTimetoBuyandSellStockIII.cpp
65 lines (55 loc) · 2.14 KB
/
123_BestTimetoBuyandSellStockIII.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
64
65
#include "stdafx.h"
//-------------------------------------------------------------------------
#include <algorithm>
class Solution {
public:
int maxProfit(vector<int> const& prices) {
if (prices.empty()) return 0;
vector<int> leftProfits(prices.size(), 0);
{
int minPrice = prices[0];
for (int i = 1; i < (int)prices.size(); ++i) {
if (prices[i] > minPrice) {
leftProfits[i] = max(leftProfits[i - 1], prices[i] - minPrice);
} else {
minPrice = prices[i];
leftProfits[i] = leftProfits[i - 1];
}
}
}
int result = 0;
int maxPrice = prices.back();
int rightProfit = 0;
for (int i = prices.size() - 2; i >= 0; --i) {
if (prices[i] < maxPrice) {
rightProfit = max(rightProfit, maxPrice - prices[i]);
} else {
maxPrice = prices[i];
}
result = max(result, (i - 1 >= 0 ? leftProfits[i - 1] : 0) + rightProfit);
}
return result;
}
};
//-------------------------------------------------------------------------
int main() {
Solution so;
cout << so.maxProfit(vector<int>{}) << endl;
cout << so.maxProfit(vector<int>{ 1 }) << endl;
cout << so.maxProfit(vector<int>{ 1, 2 }) << endl;
cout << so.maxProfit(vector<int>{ 2, 1 }) << endl;
cout << so.maxProfit(vector<int>{ 1, 2, 3, 4 }) << endl;
cout << so.maxProfit(vector<int>{ 1, 4, 3, 2 }) << endl;
cout << so.maxProfit(vector<int>{ 4, 3, 2, 1 }) << endl;
cout << so.maxProfit(vector<int>{ 4, 1, 2, 3 }) << endl;
cout << so.maxProfit(vector<int>{ 1, 2, 1, 2, 1, 2 }) << endl;
cout << so.maxProfit(vector<int>{ 0, 1, 2, 3, 0, 5 }) << endl;
cout << so.maxProfit(vector<int>{ 1, 3, 1, 2, 1, 3 }) << endl;
cout << so.maxProfit(vector<int>{ 1, 2, 1, 3, 1, 3 }) << endl;
{
vector<int> prices;
for (int i = 10000; i >= 0; --i) prices.push_back(i);
for (int i = 10000; i >= 0; --i) prices.push_back(0);
cout << so.maxProfit(prices) << endl;
}
}