-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKMostFrequentElements.cpp
More file actions
59 lines (51 loc) · 1.86 KB
/
Copy pathKMostFrequentElements.cpp
File metadata and controls
59 lines (51 loc) · 1.86 KB
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
53
54
55
56
57
58
59
// Q74 https://leetcode.com/problems/top-k-frequent-elements/
// Time: O(d log d), where d is the count of distinct elements in the array. To sort the array O(d log d) time is needed.
// Space: O(d), where d is the count of distinct elements in the array. To store the elements in HashMap O(d) space complexity is needed.
class Solution {
public:
static bool compare(pair<int,int> p1,pair<int,int> p2){ //was giving compile time error without static keyword
if(p1.second==p2.second)
return p1.first>p2.first;
return p1.second>p2.second;
}
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> m;
vector<int> ans;
// sort(nums.begin(),nums.end());
for(int i =0;i<nums.size();i++){
m[nums[i]]++;
}
vector<pair<int,int>> v(m.begin(),m.end());
sort(v.begin(),v.end(),compare);
for(int j=0;j<k;j++){
ans.push_back(v[j].first);
}
return ans;
}
};
// Time: O(k log d + d), where d is the count of distinct elements in the array
// Space: O(d)
class Solution {
public:
struct compare{
bool operator()(pair<int,int> p1,pair<int,int> p2){
if(p1.second==p2.second)
return p1.first<p2.first; // difference in condition of both the approaches
return p1.second<p2.second; // Here also
}
};
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int,int> m;
vector<int> ans;
// sort(nums.begin(),nums.end());
for(int i =0;i<nums.size();i++){
m[nums[i]]++;
}
priority_queue<pair<int,int>,vector<pair<int,int>>,compare> p(m.begin(),m.end());
for(int j=0;j<k;j++){
ans.push_back(p.top().first);
p.pop();
}
return ans;
}
};