-
Notifications
You must be signed in to change notification settings - Fork 0
/
OddEvenJump.java
44 lines (39 loc) · 1.17 KB
/
OddEvenJump.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
package gracefulsoul.problems;
import java.util.Map;
import java.util.TreeMap;
public class OddEvenJump {
// https://leetcode.com/problems/odd-even-jump/submissions/1022891491/
public static void main(String[] args) {
OddEvenJump test = new OddEvenJump();
System.out.println(test.oddEvenJumps(new int[] { 10, 13, 12, 14, 15 }));
System.out.println(test.oddEvenJumps(new int[] { 2, 3, 1, 1, 4 }));
System.out.println(test.oddEvenJumps(new int[] { 5, 1, 3, 4, 2 }));
}
public int oddEvenJumps(int[] arr) {
int length = arr.length;
boolean[] higher = new boolean[length];
boolean[] lower = new boolean[length];
higher[length - 1] = true;
lower[length - 1] = true;
TreeMap<Integer, Integer> map = new TreeMap<>();
map.put(arr[length - 1], length - 1);
for (int i = length - 2; i >= 0; i--) {
Map.Entry<Integer, Integer> next = map.ceilingEntry(arr[i]);
if (next != null) {
higher[i] = lower[next.getValue()];
}
next = map.floorEntry(arr[i]);
if (next != null) {
lower[i] = higher[next.getValue()];
}
map.put(arr[i], i);
}
int result = 0;
for (int i = 0; i < length; i++) {
if (higher[i]) {
result++;
}
}
return result;
}
}