forked from kexun/sort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxMinArrNum.java
70 lines (53 loc) · 1.18 KB
/
MaxMinArrNum.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package com.demo;
import java.util.LinkedList;
/**
* 最大值减最小值<=num的子数组数量
* @author kexun
*
*/
public class MaxMinArrNum {
public static void main(String[] args) {
int[] array = {
1,2,3,4,5,6
};
MaxMinArrNum m = new MaxMinArrNum();
int re = m.getNum(array, 2);
System.out.println(re);
}
public int getNum(int[] array, int num) {
int length = array.length;
if (num < 0) {
return 0;
}
int result = 0;
int i = 0;
int j = 0;
LinkedList<Integer> qmax = new LinkedList<Integer>();
LinkedList<Integer> qmin = new LinkedList<Integer>();
while (i < length) {
while (j < length) {
while (!qmax.isEmpty() && array[qmax.peekLast()] <= array[j]) {
qmax.removeLast();
}
qmax.addFirst(j);
while (!qmin.isEmpty() && array[qmin.peekLast()] >= array[j]) {
qmin.removeLast();
}
qmin.addLast(j);
if ((array[qmax.peekFirst()] - array[qmin.peekFirst()]) > num) {
break;
}
j++;
}
if (qmax.peekFirst() == i) {
qmax.removeFirst();
}
if (qmin.peekFirst() == i) {
qmin.removeFirst();
}
result += j - i;
i++;
}
return result;
}
}