30-Find the smallest number of K

Source: Internet
Author: User
Tags time 0

Title Description: http://ac.jobdu.com/problem.php?pid=1371
Enter n integers to find the smallest number of K. For example, enter 4,5,1,6,2,7,3,8 these 8 numbers, then the smallest 4 number is 1,2,3,4,.

The first way of thinking:
As in the previous question, using the division function in the quick sort, the dividing element is the final subscript index less than k, dividing [Index+1, n]; dividing the element to the final subscript index greater than K, dividing [0, index-1]; Find the dividing element subscript is k, stop, at this time 0 ~ K-1 is the smallest number of K

Time complexity O (n), the disadvantage is that the original array data will be modified, there is no way to process the massive data.

The second way of thinking:

Use the container, save the number of K, continue to traverse the array, when the array element is smaller than the maximum number in the container , replace the maximum number with that number, when the traversal is complete, the number of k stored in the container, that is, the smallest number of K.
We need to always get the maximum value of the K number, using the maximum heap as the container.

STL does not have the largest heap container in the box, we can use set, it is implemented with red and black tree and is ordered, insert and delete are O (LG N); The first element of the default set is the minimum value, we pass in the function function to implement the maximum heap.

Time complexity O (NLGN), Space occupied O (K)

#include <iostream>#include <set>#include <functional> //function function greater,less etc. using namespace STD;typedef  multiset<int, greater<int>> maxheap;//Max HeapvoidFindminknums (int* Nums,intKintLength, Maxheap &result) {if(nums = = NULL | | k <=0|| Length <=0|| K > Length)return;    Result.insert (Nums, Nums + K); Maxheap::iterator max_iter = Result.begin (); for(inti = k; i < length; i++) {max_iter = Result.begin ();if(Nums[i] < *max_iter)            {result.erase (max_iter);        Result.insert (Nums[i]); }    }}intMain () {intNums[] = {4,5,1,6,2,7,3,8};intLength =sizeof(nums)/sizeof(nums[0]);intK =5;    Maxheap result; Findminknums (Nums, k, length, result); for(Maxheap::iterator iter = Result.begin (); ITER! = Result.end (); iter++)cout<< *iter << Endl;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

30-Find the smallest number of K

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.