-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path188. Best Time to Buy and Sell Stock IV.cpp
174 lines (137 loc) · 5.64 KB
/
188. Best Time to Buy and Sell Stock IV.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// -----Approach 1: Memoization ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
Time: 28 ms (Beats 39.21%), Space: 17 MB (Beats 10.32%)
*/
class Solution {
private:
int sol(int idx, int buy, int limit, vector<vector<vector<int>>>& dp, vector<int>& prices){
if(limit == 0 || idx == prices.size()) return 0;
if(dp[idx][buy][limit] != -1) return dp[idx][buy][limit];
if(buy){
return dp[idx][buy][limit]= max(-prices[idx] + sol(idx+1, 0, limit, dp, prices), sol(idx+1, 1, limit, dp, prices));
} else {
return dp[idx][buy][limit]= max(prices[idx] + sol(idx+1, 1, limit-1, dp, prices), sol(idx+1, 0, limit, dp, prices));
}
}
public:
int maxProfit(int k, vector<int>& prices) {
int n= prices.size();
vector<vector<vector<int>>> dp(n, vector<vector<int>>(2, vector<int>(2*k, -1)));
return sol(0, 1, k, dp, prices);
}
};
// -----Approach 2: Memoization ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
Time: 27 ms (Beats 41.60%), Space: 14.1 MB (Beats 43.42%)
*/
class Solution {
private:
int sol(int idx, int limit, vector<vector<int>>& dp, vector<int>& prices, int k){
if(limit == 2*k || idx == prices.size()) return 0;
if(dp[idx][limit] != -1) return dp[idx][limit];
if(limit%2 == 0){
return dp[idx][limit]= max(-prices[idx] + sol(idx+1, limit+1, dp, prices, k), sol(idx+1, limit, dp, prices, k));
} else {
return dp[idx][limit]= max(prices[idx] + sol(idx+1, limit+1, dp, prices, k), sol(idx+1, limit, dp, prices, k));
}
}
public:
int maxProfit(int k, vector<int>& prices) {
int n= prices.size();
vector<vector<int>> dp(n+1, vector<int>(2*k+1, -1));
// for k=2 transacations: 2*k states where each state is completed by buy & sell
return sol(0, 0, dp, prices, k);
}
};
// -----Approach 3: Tabulation ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
Time: 42 ms (Beats 18.36%), Space: 17.1 MB (Beats 9.86%)
*/
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int n= prices.size();
vector<vector<vector<int>>> dp(n+1, vector<vector<int>>(2, vector<int>(2*k+1, 0)));
for(int idx=n-1; idx>=0; idx--){
for(int buy=0; buy<=1; buy++){
for(int limit=1; limit<=2*k-1; limit++){ // limit=0 case already covered by base case
if(buy){
dp[idx][buy][limit]= max(-prices[idx] + dp[idx+1][0][limit], dp[idx+1][1][limit]);
} else {
dp[idx][buy][limit]= max(prices[idx] + dp[idx+1][1][limit-1], dp[idx+1][0][limit]);
}
}
}
}
return dp[0][1][k];
}
};
// -----Approach 4: Tabulation ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
Time: 12 ms (Beats 82.43%), Space: 13.9 MB (Beats 56.65%)
*/
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int n= prices.size();
vector<vector<int>> dp(n+1, vector<int>(2*k+1, 0));
for(int idx=n-1; idx>=0; idx--){
for(int limit=2*k-1; limit>=0; limit--){
if(limit%2 == 0){
dp[idx][limit]= max(-prices[idx] + dp[idx+1][limit+1], dp[idx+1][limit]);
} else {
dp[idx][limit]= max(prices[idx] + dp[idx+1][limit+1], dp[idx+1][limit]);
}
}
}
return dp[0][0];
}
};
// -----Approach 5: Space Optimization ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
Time: 18 ms (Beats 64.44%), Space: 11 MB (Beats 77.94%)
*/
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int n= prices.size();
vector<vector<int>> front(2, vector<int>(2*k+1, 0));
vector<vector<int>> curr(2, vector<int>(2*k+1, 0));
for(int idx=n-1; idx>=0; idx--){
for(int limit=1; limit<=2*k-1; limit++){ // limit=0 case already covered by base case
curr[1][limit]= max(-prices[idx] + front[0][limit], front[1][limit]);
curr[0][limit]= max(prices[idx] + front[1][limit-1], front[0][limit]);
}
front= curr;
}
return front[1][k];
}
};
// -----Approach 6: Space Optimization ------------------------------------------------------------
/*
Problem Link: https://leetcode.com/problems/best-time-to-buy-and-sell-stock-iv/
Time: 12 ms (Beats 82.43%), Space: 13.9 MB (Beats 56.65%)
*/
class Solution {
public:
int maxProfit(int k, vector<int>& prices) {
int n= prices.size();
vector<int> front(2*k+1, 0), curr(2*k+1, 0);
for(int idx=n-1; idx>=0; idx--){
for(int limit=2*k-1; limit>=0; limit--){
if(limit%2 == 0){
curr[limit]= max(-prices[idx] + front[limit+1], front[limit]);
} else {
curr[limit]= max(prices[idx] + front[limit+1], front[limit]);
}
}
front= curr;
}
return front[0];
}
};