-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXYNum.java
More file actions
76 lines (61 loc) · 1.93 KB
/
XYNum.java
File metadata and controls
76 lines (61 loc) · 1.93 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package stackAndqueue;
import org.junit.Test;
import java.util.LinkedList;
/**
* Created by LXF on 2016/7/25.
*/
public class XYNum {
public XYNum() {
}
public int getNum(int[] arr, int num) {
if(arr != null && arr.length != 0) {
LinkedList qmin = new LinkedList();
LinkedList qmax = new LinkedList();
int i = 0;
int j = 0;
int res;
for(res = 0; i < arr.length; ++i) {
while(j < arr.length) {
while(!qmin.isEmpty() && arr[((Integer)qmin.peekLast()).intValue()] >= arr[j]) {
qmin.pollLast();
}
qmin.addLast(j);
while(!qmax.isEmpty() && arr[((Integer)qmax.peekLast()).intValue()] <= arr[j]) {
qmax.pollLast();
}
qmax.addLast(j);
if(arr[((Integer)qmax.getFirst()).intValue()] - arr[((Integer)qmin.getFirst()).intValue()] > num) {
break;
}
++j;
}
if(((Integer)qmin.peekFirst()).intValue() == i) {
qmin.pollFirst();
}
if(((Integer)qmax.peekFirst()).intValue() == i) {
qmax.pollFirst();
}
res += j - i;
}
return res;
} else {
return 0;
}
}
@Test
public void test() {
int[] test = new int[]{56, 8};
XYNum xynum = new XYNum();
int res = xynum.getNum(test, 150);
System.out.println(res);
}
@Test
public void test1() {
LinkedList linkedList = new LinkedList();
linkedList.add(3);
linkedList.add(4);
linkedList.add(6);
System.out.println(linkedList.getFirst());
System.out.println(linkedList.peekFirst());
}
}