In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article introduces the knowledge of "how Python returns the first K most frequent elements". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Topic description
Topic description
Gives a non-empty array of integers and returns the first k elements that occur most frequently.
Example
[1BZ], k = 2, output [1pm 2]
If n is the size of the array, an algorithm whose time complexity is less than O (n log n) is required.
Algorithm analysis
The question requires us to output the first k elements that appear most frequently, because k can be equal to n at most, so in the worst case we need to count the number of occurrences of all numbers. So the question is divided into two parts:
Count the number of occurrences of all different numbers
Find out the number with k before the number of occurrences.
For question 1, because the number may be large, we need to count with the help of HashMap, and the time complexity is O (n). For problem 2, there are many ways: a simple method is to sort all the times quickly, and then output the first k, so the time complexity is O (n log n), which does not meet the requirements of this question. We need to optimize it.
Because we only need to return k numbers in the end, we only need to maintain a small root heap of size k all the time. When the number of new numbers is greater than the minimum number in the heap, we update the heap. The time complexity is O (n log k), which meets the requirements of the topic.
Is there any way to further optimize it? Because k is equal to n at worst, n log k is not very ideal. Then we need a different method of sorting. There is a sorting method whose complexity is only related to the size of the number that needs to be sorted, and in this case, the number that needs to be sorted is at most n (a number appears n times). The answer is bucket sort! Bucket sorting is to use an array bucket to record the number of occurrences of each number, throw the number into the corresponding number of buckets, and then enumerate each bucket from back to front, taking out the elements until k are full. The time complexity is O (n).
Finally, the time complexity of the optimal algorithm is O (n).
Reference program
That's all for "how Python returns the first K most frequent elements". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.