일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Python
- iterator
- LeetCode
- Regular Expression
- Generator
- Python Code
- 파이썬
- DWG
- 315. Count of Smaller Numbers After Self
- 밴픽
- Class
- Substring with Concatenation of All Words
- 715. Range Module
- data science
- attribute
- 109. Convert Sorted List to Binary Search Tree
- 운영체제
- 컴퓨터의 구조
- t1
- Convert Sorted List to Binary Search Tree
- 프로그래머스
- concurrency
- shiba
- 43. Multiply Strings
- Protocol
- 시바견
- Python Implementation
- Decorator
- 30. Substring with Concatenation of All Words
- kaggle
Archives
- Today
- Total
Scribbling
[C++] priority queue (vector<int>) with comparator 본문
Computer Science/C++
[C++] priority queue (vector<int>) with comparator
focalpoint 2023. 8. 18. 05:52
LeetCode 347. Top K Frequent Elements
https://leetcode.com/problems/top-k-frequent-elements/
struct my_comparator
{
bool operator()(std::vector<int> const& a, std::vector<int> const& b) const
{
// smallest to top (min heap)
return a[0] > b[0];
}
};
using custom_priority_queue = std::priority_queue<std::vector<int>, std::vector<std::vector<int>>, my_comparator>;
class Solution {
public:
vector<int> topKFrequent(vector<int>& nums, int k) {
unordered_map<int, int> counter;
for (auto num : nums) {
counter[num]++;
}
custom_priority_queue pq;
for (auto e : counter) {
vector<int> tmp;
tmp.push_back(e.second);
tmp.push_back(e.first);
pq.push(tmp);
if (pq.size() > k) {
pq.pop();
}
}
vector<int> ret;
while (!pq.empty()) {
vector<int> tmp = pq.top();
pq.pop();
ret.push_back(tmp[1]);
}
return ret;
}
};
'Computer Science > C++' 카테고리의 다른 글
[C++] LeetCode 128. Longest Consecutive Sequence (0) | 2023.08.18 |
---|---|
[C++] LeetCode 238. Product of Array Except Self (0) | 2023.08.18 |
[C++] 49. Group Anagrams (0) | 2023.08.18 |
[C++] LeetCode 242. Valid Anagram (0) | 2023.08.18 |
[C++] LeetCode 217. Contains Duplicate (0) | 2023.08.18 |