-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCriticalPoint.java
More file actions
44 lines (36 loc) · 1.23 KB
/
CriticalPoint.java
File metadata and controls
44 lines (36 loc) · 1.23 KB
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 DoublyLinkedList;
import java.util.ArrayList;
import java.util.Arrays;
public class CriticalPoint {
public static boolean is_critical_point(Node node) {
if (node.next.data > node.data && node.prev.data > node.data) return true;
return node.next.data < node.data && node.prev.data < node.data;
}
public static ArrayList<Integer> findMaxMin(Node head) {
ArrayList<Integer> ans = new ArrayList<>(Arrays.asList(2000000000, 2000000000));
int first = -1, last = -1;
Node tail = head;
while (tail.next != null) tail = tail.next;
Node curr = tail.prev;
if (curr == null) return new ArrayList<>(Arrays.asList(-1, -1));
int pos = 0;
while (curr != head) {
if (is_critical_point(curr)) {
if (first != -1) {
ans.set(0, Math.min(ans.get(0), pos - last));
ans.set(1, pos - first);
last = pos;
} else {
first = last = pos;
}
}
pos++;
curr = curr.prev;
}
if (ans.get(0) == 2e9) {
ans.set(0, -1);
ans.set(1, -1);
}
return ans;
}
}