In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how LeetCode solves the problem of the first K high-frequency elements, which has a certain reference value, and interested friends can refer to it. I hope you will gain a lot after reading this article.
Title
Given a non-empty array of integers, return the elements in which k is high before the occurrence frequency.
Example 1: input: nums = [1gime 1je 1jue 2je 2je 3], k = 2 output: [1Jue 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. Train of thought
First, iterate through the entire array, use a hash table to record the number of occurrences of each number, and form an array of occurrences. Finding the first kk high-frequency elements of the original array is equivalent to finding the large pre-kk value of the occurrence number array.
The easiest thing to do is to sort the occurrence array. However, because there may be O (N) O (N) different occurrence times (where NN is the length of the original array), the total algorithm complexity will reach O (N\ logN) O (NlogN), which does not meet the requirements of the topic.
Here, we can use the idea of the heap: create a small top heap and then iterate through the "occurrence array":
If the number of elements in the heap is less than kk, it can be inserted directly into the heap.
If the number of elements in the heap is equal to kk, check the size of the top of the heap and the current number of occurrences. If the stack top is larger, it means that there are at least kk numbers.
The number of occurrences is greater than the current value, so the current value is discarded; otherwise, the top of the heap pops up and the current value is inserted into the heap.
When the traversal is complete, the elements in the heap represent the large values of the previous kk in the occurrence array
Code class Solution {public int [] topKFrequent (int [] nums, int k) {Map occ = new HashMap (); for (int num: nums) {occ.put (num,occ.getOrDefault (num,0) + 1) } PriorityQueue queue = new PriorityQueue (new Comparator () {@ Override public int compare (int [] o1, int [] O2) {return o1 [1]-O2 [1];}}); for (Map.Entry integerIntegerEntry: occ.entrySet ()) {int num = integerIntegerEntry.getKey () Int count = integerIntegerEntry.getValue (); if (queue.size () = = k) {if (queue.peek () [1] < count) {queue.poll (); queue.offer (new int [] {num,count}) }} else {queue.offer (new int [] {num,count});} int [] ret = new int [k]; for (int I = 0; I < k; iTunes +) {ret [I] = queue.poll () [0];} return ret }} Thank you for reading this article carefully. I hope the article "how to solve the problem of the first K high-frequency elements of LeetCode" shared by the editor will be helpful to everyone. At the same time, I also hope that you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
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.