In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to realize the front K high-frequency elements of Python". 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!
Title
Given a non-empty array of integers, return the elements in which k is high before the occurrence frequency.
Example 1:
Input: nums = [1, 1, 1, 1, 1, 2, 2, 3], k = 2, output: [1, 1, 2]
Example 2:
Input: nums = [1], k = 1 output: [1]
Tip:
You can assume that a given k is always reasonable and the number of different elements in the 1 ≤ k ≤ array.
The time complexity of your algorithm must be better than O (n log n), where n is the size of the array.
The question data ensures that the answer is unique, in other words, the set of the first k high-frequency elements in the array is unique.
You can return the answers in any order.
Ideas for solving problems: heap
First of all, the question is examined, which requires a non-empty array to find out the elements with high k before the frequency. The hint shows that the time complexity of the algorithm is better than O (nlogn), and because we only need to return the first k elements with the highest frequency, we do not deal with the frequency after k with the help of the heap idea, and then optimize the time complexity to O (nlogk).
Then the specific measures are as follows:
First use the hash table to count the number of occurrences of elements.
Establish a minimum heap and maintain the minimum heap:
When the number of elements in the heap is less than k, the element is inserted directly here
If the number of elements in the heap is equal to k, the occurrence frequency of the new element is compared with that of the top element of the heap. If the new element appears more frequently than the top element of the heap, pop up and insert the new element.
In the end, the k in the heap is the required answer.
The specific code implementation is as follows (here the heapq module is used directly).
From typing import Listclass Solution: def topKFrequent (self, nums: List [int], k: int)-> List [int]: hash_map = {} # occurrence frequency of hash table statistical elements for num in nums: if num not in hash_map: hash_ map [num] = 0 hash_ map [num] + = 1 # establish the minimum heap The k elements with the largest storage frequency import heapq pq = [] for key in hash_map: if len (pq)
< k: heapq.heappush(pq, (hash_map[key],key)) elif hash_map[key] >Pq [0] [0]: heapq.heapreplace (pq, (hash_ map [key], key)) # take out the elements in the smallest heap res = [] while pq: res.append (pq.pop () [1]) return res# nums = [3Med 0LJ () [1]) # nums = [4Med 1LJ Lok 1LJ 2M 3] # k = solution = Solution () # print (solution.topKFrequent (nums) K)) the content of "how to realize the first K high frequency elements of Python" ends here. Thank you for your 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.