Skip to content

Commit 188df72

Browse files
committed
Add LC 347. Top K Frequent Elements prompt and 2 solutions
1 parent 78cab87 commit 188df72

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

347. Top K Frequent Elements.js

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
3+
347. Top K Frequent Elements
4+
5+
Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in any order.
6+
7+
8+
Example 1:
9+
Input: nums = [1,1,1,2,2,3], k = 2
10+
Output: [1,2]
11+
12+
Example 2:
13+
Input: nums = [1], k = 1
14+
Output: [1]
15+
16+
17+
Constraints:
18+
1 <= nums.length <= 105
19+
-104 <= nums[i] <= 104
20+
k is in the range [1, the number of unique elements in the array].
21+
It is guaranteed that the answer is unique.
22+
23+
Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
24+
25+
*/
26+
27+
/**
28+
* @param {number[]} nums
29+
* @param {number} k
30+
* @return {number[]}
31+
*/
32+
const topKFrequent = (nums, k) => {
33+
const numsMap = {};
34+
35+
for (const num of nums) {
36+
numsMap[num] = (numsMap[num] || 0) + 1;
37+
}
38+
39+
return Object.keys(numsMap)
40+
.sort((a, b) => numsMap[b] - numsMap[a])
41+
.slice(0, k);
42+
};
43+
44+
// Revised solution in O(n) time complexity
45+
const topKFrequentFollowUp = (nums, k) => {
46+
const frequencyCounter = createfrequencyCounter(nums);
47+
const frequencyBuckets = createFrequencyBuckets(frequencyCounter);
48+
const maxFreq = Math.max(...Object.values(frequencyCounter));
49+
return gatherTopKFrequent(frequencyBuckets, maxFreq, k);
50+
};
51+
52+
const createfrequencyCounter = (nums) => {
53+
const frequencyCounter = {};
54+
for (const num of nums) frequencyCounter[num] = (frequencyCounter[num] || 0) + 1;
55+
return frequencyCounter;
56+
};
57+
58+
const createFrequencyBuckets = (frequencyCounter) => {
59+
const freqBuckets = [];
60+
61+
for (const [uniqueNum, frequency] of Object.entries(frequencyCounter)) {
62+
freqBuckets[frequency] = freqBuckets[frequency] || [];
63+
freqBuckets[frequency].push(uniqueNum);
64+
}
65+
66+
return freqBuckets;
67+
};
68+
69+
const gatherTopKFrequent = (freqBuckets, maxFreq, k) => {
70+
const mostFrequent = [];
71+
72+
for (let i = maxFreq; i >= 0; i--) {
73+
if (freqBuckets[i]) mostFrequent.push(...freqBuckets[i]);
74+
if (mostFrequent.length >= k) break;
75+
}
76+
77+
return mostFrequent.slice(0, k);
78+
};

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ LeetCode Profile: https://leetcode.com/timothyshores/
3737
|[229. Bulls And Cows](https://leetcode.com/problems/bulls-and-cows)|Medium|[JavaScript](229.%20Bulls%20And%20Cows.js)
3838
|[326. Power of Three](https://leetcode.com/problems/power-of-three/)|Easy|[JavaScript](326.%20Power%20of%20Three.js)
3939
|[345. Reverse Vowels of a String](https://leetcode.com/problems/reverse-vowels-of-a-string/)|Easy|[JavaScript](345.%20Reverse%20Vowels%20of%20a%20String.js)
40+
|[347. Top K Frequent Elements](https://leetcode.com/problems/top-k-frequent-elements/)|Medium|[JavaScript](347.%20Top%20K%20Frequent%20Elements.js)
4041
|[384. Shuffle an Array](https://leetcode.com/problems/shuffle-an-array/)|Medium|[JavaScript](384.%20Shuffle%20an%20Array.js)
4142
|[387. First Unique Character in a String](https://leetcode.com/problems/first-unique-character-in-a-string/)|Easy|[JavaScript](387.%20First%20Unique%20Character%20in%20a%20String.js)
4243
|[482. License Key Formatting](https://leetcode.com/problems/license-key-formatting/)|Easy|[JavaScript](482.%20License%20Key%20Formatting.js)

0 commit comments

Comments
 (0)