-
Notifications
You must be signed in to change notification settings - Fork 24
/
0347-top-k-frequent-elements.js
52 lines (39 loc) · 1.23 KB
/
0347-top-k-frequent-elements.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// 347. Top K Frequent Elements
// Medium 48%
// Given a non-empty array of integers, return the k most frequent elements.
// For example,
// Given [1,1,1,2,2,3] and k = 2, return [1,2].
// Note:
// - You may assume k is always valid, 1 ≤ k ≤ number of unique elements.
// - Your algorithm's time complexity must be better than O(n log n), where n
// is the array's size.
/**
* @param {number[]} nums
* @param {number} k
* @return {number[]}
*/
const topKFrequent = function(nums, k) {
const hash = {}
for (let num of nums) hash[num] = (hash[num] || 0) + 1
const bucket = Array(nums.length + 1)
for (let num in hash) {
const frequency = hash[num]
if (bucket[frequency] == null) bucket[frequency] = []
bucket[frequency].push(parseInt(num))
}
const result = []
for (let i = bucket.length - 1; i >= 0 && result.length < k; i--) {
if (bucket[i]) result.push(...bucket[i])
}
return result
}
;[
[[1,1,1,2,2,3], 2], // [1,2]
].forEach(([nums, k]) => {
console.log(topKFrequent(nums, k))
})
// Solution:
// 使用哈希表来保存数字出现的次数。
// 使用桶数组按出现次数来排序数组。
// 最后从桶数组的末尾开始取k个数。
// Submission Result: Accepted