adding another leetcode pq problem
This commit is contained in:
@@ -125,5 +125,4 @@ Lambda is new to the C++ language (part of C++11). But lambda is a core piece of
|
||||
|
||||
- [Leetcode problem 215: Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/). Solution: [p215_kth_largest_element.cpp](../../leetcode/p215_kth_largest_element.cpp).
|
||||
- [Leetcode problem 373: Find K Pairs with Smallest Sums](https://leetcode.com/problems/find-k-pairs-with-smallest-sums/). Solution: [p373_k_pairs_smallest_sums.cpp](../../leetcode/p373_k_pairs_smallest_sums.cpp).
|
||||
- [Leetcode problem 692: Top K Frequent Words](https://leetcode.com/problems/implement-stack-using-queues/). Solution: [p225_stack_using_queues.cpp](../../leetcode/p225_stack_using_queues.cpp).
|
||||
- [Leetcode problem 912: Sort an Array](https://leetcode.com/problems/implement-queue-using-stacks/). Solution: [p232_queue_using_stacks.cpp](../../leetcode/p232_queue_using_stacks.cpp).
|
||||
- [Leetcode problem 692: Top K Frequent Words](https://leetcode.com/problems/top-k-frequent-words/). Solution: [p692_top_k_frequent_words.cpp](../../leetcode/p692_top_k_frequent_words.cpp).
|
||||
|
||||
50
leetcode/p692_top_k_frequent_words.cpp
Normal file
50
leetcode/p692_top_k_frequent_words.cpp
Normal file
@@ -0,0 +1,50 @@
|
||||
class cmp{
|
||||
public:
|
||||
// this is how we overload the function call operator, and this has to a public method.
|
||||
bool operator() (std::pair<string,int> p1, std::pair<string,int> p2){
|
||||
if(p1.second==p2.second){
|
||||
// lexicographical order
|
||||
return (p1.first < p2.first);
|
||||
}else{
|
||||
// sort this way would make sure the top of the heap is the string with the lowest frequency.
|
||||
return (p1.second > p2.second);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
vector<string> topKFrequent(vector<string>& words, int k) {
|
||||
int size = words.size();
|
||||
// map words to frequency
|
||||
unordered_map<string, int> u_map1;
|
||||
vector<string> v1;
|
||||
// establish the map
|
||||
for(int i=0;i<size;i++){
|
||||
u_map1[words[i]]++;
|
||||
}
|
||||
// define a priority queue (MAX heap) with a custom comparison function
|
||||
priority_queue<pair<string, int>,vector<pair<string,int>>,cmp> pq;
|
||||
// traverse the map so as to establish the priority queue.
|
||||
unordered_map<string, int>::iterator itr;
|
||||
itr = u_map1.begin();
|
||||
while(itr != u_map1.end()){
|
||||
// push each pair into u_map1.
|
||||
pq.push(*itr);
|
||||
if(pq.size()>k){
|
||||
// evict the string with the lowest frequency?
|
||||
pq.pop();
|
||||
}
|
||||
itr++;
|
||||
}
|
||||
// after the above for loop, pq will have a size of k, and contains the strings with the highest frequency.
|
||||
// now just move these strings into v1.
|
||||
for(int i=0;i<k;i++){
|
||||
// we have to use top() here, beause pop() won't return anything.
|
||||
v1.push_back((pq.top()).first);
|
||||
pq.pop();
|
||||
}
|
||||
std:reverse(v1.begin(),v1.end());
|
||||
return v1;
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user