-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path188 Best Time to Buy and Sell Stock IV .cpp
52 lines (52 loc) · 1.71 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
class Solution {
public:
int maxProfit(int m, vector<int>& prices) {
if (!m || prices.empty()) {
return 0;
}
int n = prices.size();
using pii = pair<int, int>;
vector<pii> a;
for (int i = 0; i < n; i++) {
int j, k;
for (j = i + 1; j < n && prices[j] <= prices[j - 1]; j++);
j--;
for (k = j + 1; k < n && prices[k] >= prices[k - 1]; k++);
k--;
a.emplace_back(j, k);
// cout << j << ", " << k << endl;
i = k;
}
vector<int> trs;
stack<pii> st;
for (int i = 0; i < a.size(); i++) {
auto now = a[i];
while (!st.empty() && prices[now.first] < prices[st.top().first]) {
trs.push_back(prices[st.top().second] - prices[st.top().first]);
st.pop();
}
while (!st.empty() && prices[now.first] >= prices[st.top().first] && prices[now.second] >= prices[st.top().second]
) {
auto t = st.top();
st.pop();
trs.push_back(prices[t.second] - prices[now.first]);
now = make_pair(t.first, now.second);
}
st.push(now);
}
while (!st.empty()) {
trs.push_back(prices[st.top().second] - prices[st.top().first]);
st.pop();
}
// for (auto tr : trs) {
// cout << tr << endl;
// }
m = min(m, int(trs.size()));
nth_element(begin(trs), begin(trs) + m, end(trs), greater<int>());
int ans = 0;
for (int i = 0; i < m; i++) {
ans += trs[i];
}
return ans;
}
};