-
Notifications
You must be signed in to change notification settings - Fork 53
Expand file tree
/
Copy pathLeetCode_188_41.cpp
More file actions
39 lines (34 loc) · 942 Bytes
/
LeetCode_188_41.cpp
File metadata and controls
39 lines (34 loc) · 942 Bytes
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
class Solution
{
public:
int maxProfit(int k, vector<int> &prices)
{
if (k <= 0 || prices.size() <= 1)
return 0;
if (k > prices.size() / 2)
return maxProfixWithoutTransactionLimit(prices);
vector<int> buy(k, INT_MIN);
vector<int> sell(k, 0);
for (auto price : prices)
{
buy[0] = max(buy[0], -price);
sell[0] = max(sell[0], buy[0] + price);
for (int j = 1; j < k; j++)
{
buy[j] = max(buy[j], sell[j - 1] - price);
sell[j] = max(sell[j], buy[j] + price);
}
}
return max(sell[k - 1], 0);
}
private:
int maxProfixWithoutTransactionLimit(vector<int> &prices)
{
int result = 0;
for (int i = 1; i < prices.size(); i++)
{
result += max(prices[i] - prices[i - 1], 0);
}
return result;
}
};