add: 221019

This commit is contained in:
Eatswap 2022-10-20 01:27:33 +08:00
parent 2087868f4e
commit a7b808cb39
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 46 additions and 1 deletions

45
cpp/2210/221019.cpp Normal file
View File

@ -0,0 +1,45 @@
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_map>
#include <queue>
/**
* 692. Top K Frequent Words
*
* Given an array of strings words and an integer k, return the k most frequent strings.
* Return the answer sorted by the frequency from highest to lowest. Sort the words with the same frequency by their lexicographical order.
*/
class Solution {
public:
static std::vector<std::string> topKFrequent(const std::vector<std::string>& words, int k) {
std::unordered_map<std::string, int> m;
std::for_each(words.begin(), words.end(), [&](auto&& x) { ++m[x]; });
auto cmp = [](auto&& x, auto&& y) {
return (x.second != y.second) ? x.second > y.second : x.first < y.first;
};
using PSI = std::pair<std::string, int>;
std::priority_queue<PSI, std::vector<PSI>, decltype(cmp)> q;
std::for_each(m.begin(), m.end(), [&](auto&& x) {
q.push(x);
if (q.size() > k)
q.pop();
});
std::vector<std::string> ret;
ret.reserve(k);
while (!q.empty()) {
ret.push_back(q.top().first);
q.pop();
}
return ret;
}
};
int main() {
return 0;
}

View File

@ -3,4 +3,4 @@ PROJECT(2210)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2210 221001.cpp)
ADD_EXECUTABLE(2210 221019.cpp)