leetcode

Solution to some Leetcode problems written in C++
git clone git://git.dimitrijedobrota.com/leetcode.git
Log | Files | Refs | README | LICENSE

0347.cpp (783B)


      1 struct elem {
      2     static void reset() { id_cnt = 0; }
      3     static int id_cnt;
      4     int id = id_cnt++, count = 0;
      5     void operator++(int) { count++; }
      6     friend bool operator<(const elem &e1, const elem &e2) { return e1.count > e2.count; }
      7     friend ostream &operator<<(ostream &os, const elem &e) { return os << e.id << ": " << e.count; }
      8 };
      9 int elem::id_cnt = 0;
     10 
     11 class Solution {
     12     const int size = 20002;
     13     const int mod = 10000;
     14 
     15   public:
     16     vector<int> topKFrequent(vector<int> &nums, int k) {
     17         elem::reset();
     18         vector<elem> um(size);
     19         for (int n : nums)
     20             um[n + mod]++;
     21         sort(um.begin(), um.end());
     22         vector<int> res;
     23         for (int i = 0; i < k; i++)
     24             res.push_back(um[i].id - mod);
     25         return res;
     26     }
     27 };