Top k problem is more common, here to summarize the method.
1, with the smallest heap to do.
The idea is to use the first k numbers in the array to build a minimum heap, and then compare the remaining elements to the top of the heap, if an element is larger than the top element of the heap, replace the top element of the heap, and re-adjust to the minimum heap.
Here, the heap is actually the largest number of the first K. The top of the heap is the largest number of K. So the first k, the first k can be begged out. The code is as follows:
1 Public voidFindint[] Nums,intk) {2Priorityqueue<integer> Priorityqueue =NewPriorityqueue<>();3 for(inti = 0; I < K; i + +) Priorityqueue.offer (Nums[i]);4 for(inti = k; i < nums.length; i + + ){5 if(Nums[i] >Priorityqueue.peek ()) {6 Priorityqueue.poll ();7 Priorityqueue.offer (Nums[i]);8 }9 }Ten System.out.println (Priorityqueue.peek ()); One while(Priorityqueue.peek ()! =NULL ){ ASystem.out.print (Priorityqueue.poll () + ""); - } -}
The first line of output is the number of K, and the second line is the number of the first k large.
Now there are two thoughts: 1, this code is valid for duplicate numbers, that is, if the input is [3,4,5,6,6], the third largest number, the result is 5. But actually we want to ignore the repetition, want to output 4, what should be done? 2, if you want to find the smallest number of k should do.
If you want to ignore repetition, we add a judgment on the heap and the subsequent elements into the heap.
If you want to ask for the first k small or the small number D K, here is to understand the Java package class for the heap: Priorityqueue. This wrapper class implements the minimum heap by default and considers duplication. If you want to implement the above ideas, you need to rewrite the Compare method . Compare method is used in the process of siftup, siftup This function is to adjust upward, this function will compare the child node and father node size relationship whether meet the requirements, if satisfied, break, if not satisfied will exchange the child and father value.
1 Private voidSiftup (intK, E x) {2 if(Comparator! =NULL)3 Siftupusingcomparator (k, x);4 Else5 siftupcomparable (k, x);6 }7 Private voidSiftupusingcomparator (intK, E x) {8 while(k > 0) {9 intParent = (k-1) >>> 1;TenObject e =Queue[parent]; One if(Comparator.compare (x, (e) e) >= 0) A Break; -QUEUE[K] =e; -K =parent; the } -QUEUE[K] =x; -}
Note that if you do not override the Compare method, the default Siftupcomparable method is called (this method is the method of building the minimum heap). If overridden, the comparison method in the Compare method is used to determine whether the condition is met. I rewrote the code for the largest heap as follows:
1 New Priorityqueue<> (new comparator<integer>() {2 @Override3 public int Compare (integer O1, integer o2) {4 return o2-O1; 5 }6 });
The O1 parameter in the Compare method is the child node, and the O2 parameter is the Father node. The biggest heap here means: If the value of the parent node is greater than the value of the child's node, no adjustment is required; So the heap built on this principle is the largest heap.
Then the minimum number of K and the first K minimum code are obvious:
1 Public voidFindint[] Nums,intk) {2Priorityqueue<integer> Priorityqueue =NewPriorityqueue<> (NewComparator<integer>() {3 @Override4 Public intCompare (integer O1, integer o2) {5 returno2-O1;6 }7 });8 for(inti = 0; I < K; i + + ) 9 {Ten if( !priorityqueue.contains (Nums[i]) Priorityqueue.offer (Nums[i]); One } A for(inti = k; i < nums.length; i + + ){ - if(Nums[i] < Priorityqueue.peek () &&!priorityqueue.contains (Nums[i])) { - Priorityqueue.poll (); the Priorityqueue.offer (Nums[i]); - } - } - System.out.println (Priorityqueue.peek ()); + while(Priorityqueue.peek ()! =NULL ){ -System.out.print (Priorityqueue.poll () + ""); + } A}
Top K and Java Priorityqueue