forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1340.java
49 lines (43 loc) · 1.45 KB
/
1340.java
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
class Solution {
public int maxJumps(int[] arr, int d) {
n = arr.length; td = d;
dp = new int[n];
lb = new int[n];
rb = new int[n];
Arrays.fill(lb, -1);
Arrays.fill(rb, -1);
findBiggerLeft(arr, lb);
findBiggerRight(arr, rb);
int res = 0;
for (int i = 0; i < n; i++) {
res = Math.max(res, dfs(i));
}
return res;
}
private int[] dp, lb, rb;
private int n, td;
private void findBiggerRight(int[] arr, int[] res) {
Deque<Integer> s = new LinkedList();
for (int i = 0; i < n; i++) {
while (!s.isEmpty() && s.getFirst() < i - td) s.removeFirst();
while (!s.isEmpty() && arr[s.getLast()] < arr[i]) res[s.removeLast()] = i;
s.addLast(i);
}
}
private void findBiggerLeft(int[] arr, int[] res) {
Deque<Integer> s = new LinkedList();
for (int i = 0; i < n; i++) {
while (!s.isEmpty() && s.getFirst() < i - td) s.removeFirst();
while (!s.isEmpty() && arr[s.getLast()] <= arr[i]) s.removeLast();
if (!s.isEmpty()) res[i] = s.getLast();
s.addLast(i);
}
}
private int dfs(int u) {
if (dp[u] > 0) return dp[u];
dp[u] = 1;
if (lb[u] >= 0) dp[u] = Math.max(dp[u], dfs(lb[u]) + 1);
if (rb[u] >= 0) dp[u] = Math.max(dp[u], dfs(rb[u]) + 1);
return dp[u];
}
}