-
Notifications
You must be signed in to change notification settings - Fork 481
/
1340.cpp
59 lines (52 loc) · 1.42 KB
/
1340.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
class Solution
{
public:
int maxJumps(vector<int>& arr, int d)
{
n = arr.size(), td = d;
lb = vector<int>(n, -1);
rb = vector<int>(n, -1);
findBiggerRight(arr, rb);
findBiggerLeft(arr, lb);
dp = vector<int>(n);
int res = 0;
for (int i = 0; i < n; i++) res = max(res, dfs(i));
return res;
}
private:
vector<int> dp, lb, rb;
int n, td;
void findBiggerRight(vector<int>& arr, vector<int>& res)
{
deque<int> s;
for (int i = 0; i < n; i++)
{
while (!s.empty() && s.front() < i - td) s.pop_front();
while (!s.empty() && arr[s.back()] < arr[i])
{
res[s.back()] = i;
s.pop_back();
}
s.emplace_back(i);
}
}
void findBiggerLeft(vector<int>& arr, vector<int>& res)
{
deque<int> s;
for (int i = 0; i < n; i++)
{
while (!s.empty() && s.front() < i - td) s.pop_front();
while (!s.empty() && arr[s.back()] <= arr[i]) s.pop_back();
if (!s.empty()) res[i] = s.back();
s.emplace_back(i);
}
}
int dfs(int u)
{
if (dp[u]) return dp[u];
dp[u] = 1;
if (lb[u] >= 0) dp[u] = max(dp[u], dfs(lb[u]) + 1);
if (rb[u] >= 0) dp[u] = max(dp[u], dfs(rb[u]) + 1);
return dp[u];
}
};