Programming beauty 2.5 find the maximum K number, 2.5

Source: Internet
Author: User

Programming beauty 2.5 find the maximum K number, 2.5

Looking for the maximum number of K in an array, we will first talk about a very simple method, using the segmentation algorithm in quick sorting, that is, we often see the partition. This function returns a value of the int type. This value represents the split point between the first half of the number and the last half of the number. The first half of the number is less than or equal to the last half of the number (incremental sorting ), therefore, we only need to find the corresponding split point, that is, we can find the maximum K number, or the minimum K number, which is the method to complete the task using the linear method.

First, the function declaration is provided:

int DutPartition(int*, int, int);void DutFindMaxKInArray_1(int*, int, int);

The source code is as follows:

/* Classic fast partition Program */int DutPartition (int * A, int low, int high) {/* Sentinel. In fact, you can use rand to generate A random value, minimize efficiency */int lower = A [low]; while (low 

After analyzing the code, we can know that this method will change the order of numbers in the original array. What if we cannot change the original structure? Therefore, we can use the minimum heap to solve the TOPK problem. The minimum heap attribute is that the top element of the heap is the smallest element value in the heap. Therefore, we only need to determine whether the current heap top element is smaller than the elements in the array. If it is small, replace and re-adjust the heap. If it is large, ignore this element.

Because the stl et in stl is implemented by using the red/black tree and can implement the heap nature, we can use multiset to solve this problem. The time complexity of this method is O (nlogn)

The function declaration is as follows:

/* 2.5 find the maximum K Number */typedef std: multiset <int, std: less <int> intSet; typedef std: multiset <int, std :: less <int >>:: iteratorsetInterator; void DutFindMaxKInArray_2 (const std: vector <int> &, intSet &, int );

The source code is as follows:

/* The first solution is not good because it changes the sequence of the original number in the array * // ** the second solution uses the minimum heap idea to find the TOPK, it is a classic big data solution. * There are many similar explanations on the network. I will not explain it too much here */void DutFindMaxKInArray_2 (const std: vector <int> & data, /* simulate the minimum heap */intSet & leastNums, int k) {leastNums. clear (); if (k <1 | data. size () <k) return; vector <int>: const_iterator iter = data. begin (); for (; iter! = Data. end (); ++ iter) {if (leastNums. size () <k) {leastNums. insert (* iter);} else {setInterator it = leastNums. begin (); if (* iter> * it) {leastNums. erase (it); leastNums. insert (* iter );}}}}



What is the algorithm idea for the first few digits in a large array?

In section 2.5 of the beauty of programming, you have a detailed answer.
A better solution is to build the maximum or minimum heap.

The difference between the number of electrons in the L-layer and the number of electrons in the K-layer is 25 times that of the number of electrons in the C-element atom.

The number of K-layer electrons is 2.
X-2 = 2.5 * n
When n = 2, x = 7, that is, 27 F Fluorine

Related Article

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.