From 198fe01d8ce65fc676447f9ba3c2dbee49a188fe Mon Sep 17 00:00:00 2001 From: Jidong Xiao Date: Tue, 14 Nov 2023 00:52:53 -0500 Subject: [PATCH] adding another leetcode pq problem --- lectures/22_priority_queues/README.md | 3 +- leetcode/p692_top_k_frequent_words.cpp | 50 ++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 leetcode/p692_top_k_frequent_words.cpp diff --git a/lectures/22_priority_queues/README.md b/lectures/22_priority_queues/README.md index 2840926..5cc8898 100644 --- a/lectures/22_priority_queues/README.md +++ b/lectures/22_priority_queues/README.md @@ -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). diff --git a/leetcode/p692_top_k_frequent_words.cpp b/leetcode/p692_top_k_frequent_words.cpp new file mode 100644 index 0000000..9bfec55 --- /dev/null +++ b/leetcode/p692_top_k_frequent_words.cpp @@ -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 p1, std::pair 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 topKFrequent(vector& words, int k) { + int size = words.size(); + // map words to frequency + unordered_map u_map1; + vector v1; + // establish the map + for(int i=0;i,vector>,cmp> pq; + // traverse the map so as to establish the priority queue. + unordered_map::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