-
Notifications
You must be signed in to change notification settings - Fork 0
/
714_buySellStockWithFee.cpp
67 lines (49 loc) · 1.5 KB
/
714_buySellStockWithFee.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
66
67
#include <iostream>
#include <vector>
#include <limits.h>
using namespace std;
int maxProfit(vector<int>& prices)
{
int profit = 0;
int size = prices.size();
for (int i = 1; i != size; ++i) {
if (prices[i] > prices[i-1]) {
profit += prices[i] - prices[i-1];
}
}
return profit;
}
int maxProfit(vector<int>& prices, int fee)
{
int size = prices.size();
int profit = 0;
// vector<vector<int>> result(2, vector<int>(2, INT_MIN));
// result[0][0] = 0;
// result[0][1] = -prices[0];
// for (int i = 1; i < size; ++i) {
// int m = i % 2;
// int n = (i+1) % 2;
// result[m][0] = max(result[n][0], result[n][1] + prices[i] - fee);
// result[m][1] = max(result[n][1], result[n][0] - prices[i]);
// profit = max(profit, result[m][0]);
// cout << "i = " << i << " profit = " << profit << endl;
// }
int hold = -prices[0];
for (int i = 1; i < size; ++i) {
/*
* We can transform profit first without using temporary variables because
* selling and buying on the same day can't be better than just continuing to hold the stock.
*/
// int tmp = profit;
profit = max(profit, hold + prices[i] - fee);
hold = max(hold, profit - prices[i]);
}
return profit;
}
int main(int argc, char const *argv[])
{
vector<int> prices = {1, 3, 2, 8, 4, 9};
int ret = maxProfit(prices, 2);
cout << "result = " << ret << endl;
return 0;
}