-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path309_buySellStockCoolTime.cpp
47 lines (33 loc) · 1018 Bytes
/
309_buySellStockCoolTime.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
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
int maxProfit(vector<int>& prices)
{
int size = prices.size();
if (2 > size) {
return 0;
}
int profit = 0;
bool coolDown = false;
vector<vector<vector<int>>> result(2, vector<vector<int>>(2, vector<int>(2, INT_MIN)));
result[0][0][0] = 0;
result[0][1][0] = -prices[0];
for (int i = 1; i != size; ++i) {
int m = i % 2;
int n = (i+1) % 2;
result[m][0][0] = max(result[n][0][0], result[n][0][1]);
result[m][1][0] = max(result[n][0][0] - prices[i], result[n][1][0]);
result[m][0][1] = result[n][1][0] + prices[i];
profit = max(profit, max(result[m][0][0], result[m][0][1]));
cout << "i = " << i << " profit = " << profit << endl;
}
return profit;
}
int main(int argc, char const *argv[])
{
vector<int> prices = {1, 2, 3, 0, 2};
int ret = maxProfit(prices);
cout << "result = " << ret << endl;
return 0;
}