/ **
*
* Question:
* less than or equal the maximum minus the minimum number of sub-arrays num
* num integer to an array arr and fixed, how many children were returned array satisfy the following cases:
* 1.max (arr [I. . J]) - min (arr [i..j]) <= NUM
* 2.max (arr [i..j]) represents the subarray arr [i..j] maximum value
* 3.min ( arr [i..j]) represents the subarray arr [i..j] minimum value
*
* requirements:
* If the length of the array is N, implement a time complexity of O (N) of the solution.
*
* Answer:
* first introduced common solution to find all of the sub-array arr, a total of O (N ^ 2) months, then for each sub-array do traverse to find where the minimum and maximum values, and this process
* time complexity is O (N), then look at the sub-array meets the condition. Statistics on the number of all sub-array can meet the conditions. Ordinary solution is easy to implement, but the time complexity is
* O (N ^ 3), to achieve the desired O (N) time complexity required deque.
*
* Generates two deque qmax and qmin. When the subarray is arr [i..j], qmax maintenance window subarray arr [i..j] to update the maximum value of the structure, qmin maintenance
* window subarray arr [i..j] minimum update structure.
* When the number of subgroups arr [i..j] a right position becomes enlarged arr [i..j + 1], the average time updating cost qmin and qmax structural complexity is O (1), and may then O (1) is
* Obtained arr [i..j + 1] of the maximum and minimum time.
* When the subarray arr [i..j] position to the left into a diffuser arr [i + 1..j] time, updating cost qmin and qmax average time complexity of the structure of O (1), and may then O (1) is
obtained arr [i + 1..j] * of the maximum and minimum time.
*
* Met by the subject analysis condition, two conclusions can be obtained.
*
* 1. If the subarray arr [i..j] satisfies the condition, that is, max (arr [i..j]) - min (arr [i..j]) <= num, then arr [i .. j] in each sub-
* array, i.e. arr [k..l] (i <= k <= l <= j) satisfies a condition, we subarray arr [i..j-1] as an Example DESCRIPTION maximum arr [i..j-1] may be only a small
* than or equal to the maximum arr [i..j] a, the minimum value of arr [i..j-1] may be greater than or equal to only arr [i..j] minimum, so than or equal ARR [-i..j. 1]
* necessarily satisfy the condition. Similarly, each sub-array arr [i..j] satisfy the conditions.
* 2. If the sub-array arr [i..j] condition is not satisfied, then all contain each sub-array arr [i..j] are not met conditions. Namely ARR [k..l] (K <= I <= J <= K)
*
* Code Design:
* 1 generates deque qmax and Qmin. Generating two integer variables i and j, represents the range of sub-arrays, namely arr [i..j]. Generate integer variable res, it represents all satisfy the conditions of the sub-array
* number.
* 2. Let j continue to move to the right (j ++), represents arr [i..j] expanding to the right, and constantly updated qmax and qmin structure to ensure qmax and qmin always maintaining the maximum and minimum dynamic window
* values update structure where once arr [i..j] does not satisfy the condition occurs, j of the expansion process is stopped at this time right arr [i..j-1], arr [i..j-2], ... ARR [i..i +. 1],
* ARR [i..i] certain conditions are satisfied, all to arr [i] as a sub-element of the first array, is a number satisfying the condition j-1 th. So-so. 1 J = + RES.
*. 3. When the step 2 has been completely, so that the right one position i, and qmin and qmax updated accordingly, then repeat steps 2 to.
* *
Analysis:
* The code design process, all the index values up into qmax and qmin once a qmax and qmin time, the value of i and j is increasing, and never reduced, so the whole
time * Code complexity is O (N).
*
* @Author snow pupil
*
* /
import java.util.LinkedList;
public class getNum {
public static int getNumArray(int arr[],int num) {
if(arr == null || arr.length == 0 || num < 0) {
return 0;
}
LinkedList<Integer> qmax = new LinkedList<>();
LinkedList<Integer> qmin = new LinkedList<>();
int i = 0;
int j = 0;
int res = 0;
//假设数组上一遍历状态为i..j,则i+1..j是都可以满足条件要求的,下一遍历状态可能达到i+1..k(k>j)
while(i<arr.length) {
while(j<arr.length) {
if(qmin.isEmpty() || qmin.peekLast() !=j) {
//自小变大
while(!qmin.isEmpty() && arr[qmin.peekLast()]>=arr[j]) {
qmin.pollLast();
}
qmin.addLast(j);
//自大变小
while(!qmax.isEmpty() && arr[qmax.peekLast()]<=arr[j]) {
qmax.pollLast();
}
qmax.addLast(j);
}
//跳出
if(arr[qmax.getFirst()]-arr[qmin.getFirst()] > num) {
break;
}
j++;
}
res += j-i;
//弹出队列首元素
if(qmin.peekFirst() == i) {
qmin.pollFirst();
}
if(qmax.peekFirst() == i) {
qmax.pollFirst();
}
i++;
}
return res;
}