leetcode

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

1338.cpp (572B)


      1 class Solution {
      2   public:
      3     int minSetSize(vector<int> &arr) {
      4         sort(begin(arr), end(arr));
      5 
      6         vector<int> count;
      7         int n = arr.size() / 2, crnt = 1, res = 0;
      8         for (int i = 1; i < arr.size(); i++) {
      9             if (arr[i] == arr[i - 1])
     10                 crnt++;
     11             else {
     12                 count.push_back(crnt);
     13                 crnt = 1;
     14             }
     15         }
     16 
     17         count.push_back(crnt);
     18         sort(rbegin(count), rend(count));
     19 
     20         for (int i = 0; n > 0; i++)
     21             n -= count[i], res++;
     22         return res;
     23     }
     24 };