-
Notifications
You must be signed in to change notification settings - Fork 481
/
0975.cpp
43 lines (42 loc) · 1.04 KB
/
0975.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
class Solution
{
public:
int oddEvenJumps(vector<int>& A)
{
int n = A.size();
vector<int> d(n);
for (int i = 0; i < n; i++) d[i] = i;
stable_sort(d.begin(), d.end(), [&](const int i, const int j){
return A[i] < A[j];
});
vector<int> n1 = st(d);
stable_sort(d.begin(), d.end(), [&](const int i, const int j){
return A[i] > A[j];
});
vector<int> n2 = st(d);
vector<int> h(n), l(n);
h[n-1] = l[n-1] = 1;
for (int i = n - 2; i >= 0; i--)
{
h[i] = l[n1[i]];
l[i] = h[n2[i]];
}
return accumulate(h.begin(), h.end(), 0);
}
private:
vector<int> st(vector<int>& data)
{
int n = data.size();
vector<int> s, res(n);
for (int i : data)
{
while (!s.empty() and s.back() < i)
{
res[s.back()] = i;
s.pop_back();
}
s.push_back(i);
}
return res;
}
};