Maximum minus the minimum value less than the number of sub-arrays equal to num
Given the array arr and integer num, total returns how many arrays meet the following conditions: Max (arr[i. J])-min (arr[i). J]) <=num. where Max (arr[i. J]) represents a subarray of arr[i. J] The maximum value, min (arr[i). J]) represents a subarray of arr[i. The minimum value in J]. If the length of the array is n, the time complexity is required to be O (n).
Resolution
Using a double-ended queue, Qmax maintains the window sub-array arr[i. J] The maximum value of the updated structure, Qmin maintains the window sub-array arr[i. J] The minimum value of the updated structure, all subscript values are up to Qmax and Qmin once, out of Qmax and Qmin once, and the values of I and J are also increasing, never decreasing, so the time complexity is O (N).
The following two conclusions can be obtained through the analysis of the topic:
1) If the sub-array arr[i: J] satisfies the condition, that is, Max (arr[i. J])-min (arr[i). J]) <=num, then arr[k. L] (I<=K<=L<=J) must satisfy the condition, even if an array satisfies the condition, all its sub-arrays must satisfy the condition.
2) If the sub-array arr[i: J] does not satisfy the condition, that is, Max (arr[i. J])-min (arr[i). J]) >num, then arr[k. L] (k<=i<=j<=l) certainly does not satisfy the condition, even if an array does not satisfy the condition, all the array containing it must not satisfy the condition.
Packagecom.test;Importjava.util.LinkedList;/*** Created by DEMRYSTV. * * All subscript values are up to Qmax and Qmin once, out of Qmax and qmin once, the values of I and J are increasing, never decreasing, so the time complexity is O (N)*/ Public classGetnum { Public intGetnum (int[] arr,intnum) { if(Arr.length==0 | | arr==NULL){ return0; } //double-ended queue, Qmax maintains the window sub-array arr[i. J] The maximum value of the updated structure, Qmin maintains the window sub-array arr[i. J] The minimum value of the updated structure,//if an array satisfies the criteria, then the sub-arrays it contains must all satisfy the condition//If an array does not satisfy the condition, then all arrays containing it must not satisfy the conditionLinkedlist<integer> Qmax =NewLinkedlist<integer>(); LinkedList<Integer> qmin =NewLinkedlist<integer>(); inti = 0; intj = 0; intres = 0; while(i<arr.length) { while(j<arr.length) { 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++; } //double-ended queue the team head to the starting point, he bounced out if(Qmin.peekfirst () = =i) {Qmin.pollfirst (); } if(Qmax.peekfirst () = =i) {Qmax.pollfirst (); } //the use of the conclusion is if arr[i. J] Meet the conditions, then arr[i. J-1] must meet the conditions, if arr[i. J] satisfies the condition, then contains the arr[i. J] Array must not satisfy the conditionRes + = J-i; I++; } returnRes; }}
Stack and queue----maximum value minus the minimum number of sub-arrays equal to num