forked from 7oSkaaa/LeetCode_DailyChallenge_2023
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
709220b
commit 364f919
Showing
1 changed file
with
50 additions
and
0 deletions.
There are no files selected for viewing
50 changes: 50 additions & 0 deletions
50
05- May/22- Top K Frequent Elements/22- Top K Frequent Elements (Ahmed Gamal).cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Author: Ahmed Gamal | ||
|
||
// for this problem we will use bucket sort to sort the elements according to their frequency (we will use counting sort to count the frequency of each element) | ||
// we can use counting sort because the elements are in a range from -10^4 to 10^4 so we can use an array of size 2*10^4+1 to count the frequency of each element | ||
|
||
// after counting the frequency of each element we will put each element in a bucket according to its frequency | ||
// then we will start from the end of the buckets (the bucket with the highest frequency) and we will put the elements in the answer vector until we reach the k elements | ||
|
||
class Solution { | ||
public: | ||
vector<int> topKFrequent(vector<int>& nums, int k) { | ||
// sf: shift factor (to shift the elements to the right to make them non-negative) | ||
// freq: array to count the frequency of each element | ||
const int sf = 1e4; | ||
vector<int> freq(sf << 1 | 1); | ||
|
||
// counting frequency of each element | ||
for(auto& i : nums) { | ||
freq[i + sf]++; | ||
} | ||
|
||
// c: buckets to put the elements in according to their frequency | ||
vector<vector<int>> c(sf + 1); | ||
|
||
// putting the elements in the buckets according to their frequency | ||
// remember that the elements are shifted to the right by sf so we need to shift them back to the left | ||
for(int i = 0; i < freq.size(); i++) { | ||
c[freq[i]].emplace_back(i - sf); | ||
} | ||
|
||
// ans: the answer vector | ||
// i: the index of the bucket we are currently in | ||
vector<int> ans; | ||
int i = c.size() - 1; | ||
|
||
// putting the elements in the answer vector until we reach the k elements | ||
while(ans.size() < k) { | ||
// if the bucket is empty we will move to the previous bucket, otherwise we will put the elements in the answer vector | ||
while(not c[i].empty() and ans.size() < k) { | ||
ans.emplace_back(c[i].back()); | ||
c[i].pop_back(); | ||
} | ||
if(c[i].empty()) { | ||
--i; | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
}; |