Prove safety offer: a minimum number of k (java)

Title: 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

Solution a: O (n) algorithm can be used only when we can modify the input array

From the previous question, we can be inspired, we can also based on Partition function to solve this problem. If the adjusted based on the k-th array of numbers, so that all the numbers than the k-th numeric are located to the left of the array, all numbers larger than the first k digits are located to the right of the array. With this adjustment, k digits located on the left in the array is the smallest k digits.

public int partition(int[] arr, int left, int right) {  
        int result = arr[left];  
        if (left > right)  
            return -1;  
  
        while (left < right) {  
            while (left < right && arr[right] >= result) {  
                right--;  
            }  
            arr[left] = arr[right];  
            while (left < right && arr[left] < result) {  
                left++;  
            }  
            arr[right] = arr[left];  
        }  
        arr[left] = result;  
        return left;  
    }  
    public int[] getLeastNumbers(int[] input,int k){  
        if(input.length == 0 || k<= 0)  
            return null;  
        int[] output = new int[k];  
        int start = 0;  
        int end = input.length-1;  
        int index = partition(input,start,end);  
        while(index != k-1){  
            if(index > k-1){  
                end = index -1;  
                index = partition(input,start ,end);  
            }  
            else{  
                start = index+1;  
                index = partition(input,start ,end);  
            }  
        }  
        for(int i = 0;i<k;i++){  
            output[i] = input[i];  
        }  
        return output;  
    }  

Solution two: O (nlogk) algorithm is particularly suitable process vast amounts of data

    We can create a data container of size k k to store the minimum number, then we read each time from a number of inputs n integers. If you already have a digital fewer than k container directly to the integer read into the container; if the container has been k digits, that is, the container is full, then we can no longer insert new figures but only to replace the existing numbers. It has to find the maximum number of k, and then take the integer and maximum values ​​to be inserted for comparison. If the value is smaller than be inserted prior to the maximum current, the maximum current has been replaced by this number; if the value to be inserted is larger than the conventional maximum current, then the smallest possible number k one of integers, so we can abandon this integer.

    This data container, we can easily think of using the maximum heap. Maximum stack, the value of the root node is always greater than the value of any node in its subtree. So we can each (1) has the maximum value obtained in the k numbers O, understand requires O (logk) complete delete, and insert operations.

  public void buildMaxHeap(int[] arr,int lastIndex){  
        for(int i = (lastIndex-1)/2;i>=0;i--){  
            int k = i;  
            while(2*k+1 <= lastIndex){  
                int biggerIndex = 2*k+1;  
                if(biggerIndex <lastIndex){  
                    if(arr[biggerIndex]< arr[biggerIndex+1])  
                        biggerIndex++;  
                }  
                if(arr[k] < arr[biggerIndex]){  
                    swap(arr,k,biggerIndex);  
                    k = biggerIndex;  
                }  
                else  
                    break;  
            }  
        }  
    }  
    public static void swap(int[] arr,int i ,int j){  
        int temp = arr[i];  
        arr[i] = arr[j];  
        arr[j] = temp;  
    }  
    public void heapSort(int[] arr){  
        for(int i = 0;i<arr.length-1;i++){  
            buildMaxHeap(arr,arr.length-i-1);  
            swap(arr,0,arr.length-i-1);  
        }  
    }  
    public void getLeastNumbers(int[] arr,int k){  
        if(arr == null || k<0 || k>arr.length)  
            return;  
        //根据输入数组前k个数简历最大堆  
        //从k+1个数开始与根节点比较  
        //大于根节点,舍去  
        //小于,取代根节点,重建最大堆  
        int[] kArray = Arrays.copyOfRange(arr, 0, k);  
        heapSort(kArray);  
        for(int i = k;i<arr.length;i++){  
            if(arr[i]<kArray[k-1]){  
                kArray[k-1] = arr[i];  
                heapSort(kArray);  
            }  
        }  
        for(int i:kArray)  
            System.out.print(i);  
    }  




Published 118 original articles · won praise 35 · views 120 000 +

Guess you like

Origin blog.csdn.net/abc7845129630/article/details/52734324