[LeetCode] 347. Top K Frequent Elements

First K-frequency elements. Meaning of the questions is to give a non-empty array, return to the first K-frequency elements. Topics requirements must be better than the time complexity O (nlogn) . example,

Example 1:

Input: nums = [1,1,1,2,2,3], k = 2
Output: [1,2]

Example 2:

Input: nums = [1], k = 1
Output: [1]

Because the topics are time complexity requirements can only be thought bucket sort bucket sort of do. The time complexity of this problem slightly better than Java JS, because different implementations of lead.

Java time O (n), JS time O (logn)

Space O (n)

Explain the idea, since it is a bucket sort, it needs to be the same frequency elements appear into the same bucket inside, so there will use a hashmap record numbers and their respective frequency of occurrence (key: value). Then you need the maximum frequency of the first K element size, according pick. Inside the JS, map can be sorted according to the value (line 21), after the completion of sorting, sorting may result, in turn, then picked before the K key (24 lines). Because the line 21 and uses a fast row quick sort, so the time complexity of at least O (logn).

 1 /**
 2  * @param {number[]} nums
 3  * @param {number} k
 4  * @return {number[]}
 5  */
 6 var topKFrequent = function (nums, k) {
 7     // corner case
 8     if (!nums || !nums.length) {
 9         return [];
10     };
11 
12     // normal case
13     let map = new Map();
14     nums.forEach((item) => {
15         if (map.has(item)) {
16             map.set(item, map.get(item) + 1);
17         } else {
18             map.set(item, 1);
19         }
20     })
21     let res = [...map].sort((a, b) => b[1] - a[1]);
22     console.log(res);
23     res = res.map(item => item[0]);
24     return res.slice(0, k);
25 };

 

In Java implementations of this question is slightly different. Still we need to create a record hashmap element and its frequency (line 9). After creating the need to use a bucket list, on each index attached to a linkedlist, recording element which has the same frequency (12 lines). Since the tub is the tub ordered ordered according to the frequency, so that when the final output can be in descending order according to the frequency of the tub barrel elements into the result set (20 lines).

 1 class Solution {
 2     public List<Integer> topKFrequent(int[] nums, int k) {
 3         HashMap<Integer, Integer> map = new HashMap<>();
 4         for (int num : nums) {
 5             map.put(num, map.getOrDefault(num, 0) + 1);
 6         }
 7 
 8         List<Integer>[] bucket = new List[nums.length + 1];
 9         for (int num : map.keySet()) {
10             int freq = map.get(num);
11             if (bucket[freq] == null) {
12                 bucket[freq] = new LinkedList<>();
13             }
14             bucket[freq].add(num);
15         }
16 
17         List<Integer> res = new ArrayList<>();
18         for (int i = bucket.length - 1; i >= 0 && res.size() < k; i--) {
19             if (bucket[i] != null) {
20                 res.addAll(bucket[i]);
21             }
22         }
23         return res;
24     }
25 }

 

Guess you like

Origin www.cnblogs.com/aaronliu1991/p/12440505.html