-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuyAndSellAtMostKTransactionLecture38.cpp
More file actions
58 lines (58 loc) · 1.55 KB
/
Copy pathBuyAndSellAtMostKTransactionLecture38.cpp
File metadata and controls
58 lines (58 loc) · 1.55 KB
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
#include <iostream>
#include <vector>
using namespace std;
//https://bit.ly/346R72e
int solve1(vector<int>& prices, int n,int k)
{
vector<vector<int>> after(2,vector<int>(k + 1,0));
vector<vector<int>> cur(2,vector<int>(k + 1,0));
for(int ind = n-1;ind >= 0;ind--)
{
for(int buy = 0;buy <= 1;buy++)
{
for(int cap = 1;cap <= k;cap++)
{
if(buy)
{
cur[buy][cap] = max(-prices[ind] + after[0][cap],
after[1][cap]);
}
else
{
cur[buy][cap] = max(prices[ind] + after[1][cap - 1],
after[0][cap]);
}
}
after = cur;
}
}
return after[1][k];
}
int solve2(vector<int>& prices, int n,int k)
{
vector<int> after(2*k + 1,0);
vector<int>cur (2*k + 1,0);
for(int ind = n-1;ind >= 0;ind--)
{
for(int trans = 2*k - 1;trans >= 0;trans--)
{
if(trans%2 == 0)
{
cur[trans] = max(-prices[ind] + after[trans + 1],
after[trans]);
}
else
{
cur[trans] = max(prices[ind] + after[trans + 1],
after[trans]);
}
after = cur;
}
}
return after[0];
}
int maximumProfit(vector<int> &prices, int n, int k)
{
return solve1(prices,n,k);
// return solve2(prices,n,k);
}