40, the smallest number k

1, Title Description: input n integers, find the smallest number K. 4,5,1,6,2,7,3,8 e.g. eight digital inputs, the minimum number is four 1,2,3,4 ,.

2, thinking: This question has three solutions, namely, sorting, divide and conquer, the maximum heap (priority queue)

Method a: Method for sorting, sorting the first array input, sorting the fastest time complexity is o (nlogn), then taking the first k number of sorted. The total time complexity is o (nlogn).

Solution two: divide and conquer method, imitate quick sort of thought, each time taking the number of k-array, and then will be smaller than the number of k-number on the left side of the array, larger than the first number on the array number k right. Then continue to adjust down. The time complexity is O (n).

  This method is not suitable for large amounts of data, because of the need to change the structure of the array, and the entire array should be loaded into memory, if the memory is not large enough, can not.

Solution three: the maximum heap (priority queue). Create a first capacity priority queue k, passing the comparator, which compare override methods to ensure that each element is ejected from the queue is the maximum. Thus the number of elements in the priority queue is less than k times, simply taken out of the array of numbers can be inserted directly into a queue; element takes precedence when the queue is full, the elements need only compare top of the stack and elements to be inserted, if heap less than custom elements to be inserted element, then give up, if elements to be inserted into the heap than the small set of elements, it would remove the heap book element, the array element is inserted.

  The advantage of this approach is that the array structure does not change, a small memory footprint, adapted to process huge amounts of data, obtaining the maximum k numbers from the maximum stack time complexity is o (1), the operation of delete and insert elements the time complexity is o (logk). Thus the overall time complexity of this method is o (nlogk). Than but almost two Solution suitable for mass data storage.

 

3, Code: direct copy, the latter need to achieve their own

import java.util.ArrayList;
import java.util.PriorityQueue;
import java.util.Comparator;
public class Solution {
   public ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k) {
       ArrayList<Integer> result = new ArrayList<Integer>();
       int length = input.length;
       if(k > length || k == 0){
           return result;
       }
        PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(k, new Comparator<Integer>() {
 
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2.compareTo(o1);
            }
        });
        for ( int I = 0; I <length; I ++ ) {
             IF (! maxHeap.size () = k) { 
          // maximum stack the k element does not directly inserted maxHeap.offer(input[i]); }
The else IF (maxHeap.peek ()> INPUT [I]) {
          // Maximum filled relatively top of the stack elements and elements to be inserted
          @ small elements to be inserted, then the top of the stack is the largest element to delete the the stack element is inserted
Integer TEMP
= maxHeap.poll (); temp = null; maxHeap.offer(input[i]); } } for (Integer integer : maxHeap) { result.add(integer); } return result; } }

Guess you like

Origin www.cnblogs.com/guoyu1/p/12164785.html