-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamazon.cpp
72 lines (57 loc) · 1.41 KB
/
amazon.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
#include <bits/stdc++.h>
using namespace std;
int stockSpike(vector<int> prices, int k) {
int n = prices.size();
set<int> prev;
set<int> next;
vector<int> p, narr;
int result = 0;
for (int i = 0; i < n; i++) {
if (prev.size() < k) {
prev.insert(prices[i]);
} else {
auto max_prev = prev.rbegin();
if (prices[i] > *max_prev) {
p.push_back(prices[i]);
}
prev.insert(prices[i]);
prev.erase(prev.begin());
}
}
for (int i = n - 1; i >= 0; i--) {
if (next.size() < k) {
next.insert(prices[i]);
} else {
auto max_next = next.rbegin();
if (prices[i] > *max_next) {
narr.push_back(prices[i]);
}
next.insert(prices[i]);
next.erase(next.begin());
}
}
map<int, int> temp;
for (int i = 0; i < p.size(); i++) {
temp[p[i]]++;
}
for (int i = 0; i < narr.size(); i++) {
if (temp[narr[i]] > 0) {
result++;
}
}
return result;
}
int main() {
int prices_count;
cin >> prices_count;
vector<int> prices(prices_count);
for (int i = 0; i < prices_count; i++) {
cin >> prices[i];
}
int k;
cin >> k;
int result = stockSpike(prices, k);
cout << result << "\n";
return 0;
}
// correct this code