Post

leetcode(리트코드)347-Top K Frequent Elements

leetcode 347 - Top K Frequent Elements 문제입니다.

1. 문제

https://leetcode.com/problems/top-k-frequent-elements/


2. Input , Output


3. 분류 및 난이도

Medium 난이도 문제입니다.
leetcode Top 100 Liked 문제입니다.


4. 문제 해석

  • 배열이 들어오고 k가 들어옵니다.
  • k는 저장공간의 크기이고, 가장 많이 반복된 숫자들을 k만큼의 저장공간에 넣어 리턴합니다.
  • map과 pq를 써야한다는 것은 알았지만 코드에 담아내지 못해 discuss를 참조했습니다. (그냥 못했다는 소리)

5. code

c++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int,int> um;
        for(size_t i = 0 ; i<nums.size();++i)
            um[nums[i]]++;
        vector<int> res;
        
        priority_queue<pair<int,int>> pq;
        
        for(auto it = um.begin(); it!=um.end(); ++it){
            pq.push(make_pair(it->second,it->first));
            if(pq.size() > (int)um.size() - k){
                res.push_back(pq.top().second);
                pq.pop();
            }
        }
        
        return res;
    }
};

6. 결과 및 후기, 개선점

c++ 90%

This post is licensed under CC BY 4.0 by the author.