Odd Even Jump LeetCode solution in Java

1
class Solution {
2
public int oddEvenJumps(int[] arr) {
3

4
int len = arr.length;
5
int minjmp[] = new int[len];
6
int maxjmp[] = new int[len];
7

8
TreeMap<Integer, Integer> map = new TreeMap<>();
9
int evjmp, oddjmp;
10
for (int i = len - 1; i >= 0; i--) {
11
Integer minpos = map.floorKey(arr[i]);
12
evjmp = (minpos != null) ? map.get(minpos) : len; // default len, to show not possible
13

14
if (evjmp != len && (evjmp == len - 1 || maxjmp[evjmp] == len - 1))
15
evjmp = len - 1; // check the last pos reachability
16

17
Integer maxpos = map.ceilingKey(arr[i]);
18
oddjmp = (maxpos != null) ? map.get(maxpos) : len;
19

20
if (oddjmp != len && (oddjmp == len - 1 || minjmp[oddjmp] == len - 1))
21
oddjmp = len - 1; // check the last pos reachability
22

23
minjmp[i] = evjmp; // specify possible jump path, if not possible assign len
24
maxjmp[i] = oddjmp; // specify possible jump path, if not possible assign len
25

26
map.put(arr[i], i); // put the current index
27
}
28

29
int res = 0;
30

31
for (int i = 0; i < len - 1; i++) {
32

33
if (maxjmp[i] == len - 1) res++;
34
}
35

36
return res + 1; // since last position will always be the answer
37
}
38
}

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0