leetcode

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

1170.cpp (795B)


      1 class Solution {
      2     int f(const string &s) {
      3         int res = 0, mini = s[0];
      4         for (const char c : s) {
      5             if (c == mini) res++;
      6             if (c < mini) {
      7                 mini = c;
      8                 res = 1;
      9             }
     10         }
     11         return res;
     12     }
     13 
     14   public:
     15     vector<int> numSmallerByFrequency(const vector<string> &queries, const vector<string> &words) {
     16         vector<int> res(queries.size()), mem(words.size());
     17         for (int i = 0; i < words.size(); i++)
     18             mem[i] = f(words[i]);
     19         sort(mem.begin(), mem.end());
     20 
     21         for (int i = 0; i < queries.size(); i++) {
     22             const int t = f(queries[i]);
     23             res[i] = words.size() - distance(begin(mem), upper_bound(begin(mem), end(mem), t));
     24         }
     25 
     26         return res;
     27     }
     28 };