generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.cpp
40 lines (36 loc) · 1.06 KB
/
solution.cpp
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
32
33
34
35
36
37
38
39
40
#include <algorithm>
#include <string>
#include <string_view>
#include <vector>
class Solution {
public:
std::vector<int> numSmallerByFrequency(std::vector<std::string>& queries, std::vector<std::string>& words) {
std::vector<int> frequencies(words.size());
for (int i = words.size() - 1; i >= 0; --i) {
frequencies[i] = calculateFrequency(words[i]);
}
std::sort(frequencies.begin(), frequencies.end());
std::vector<int> output(queries.size());
for (int i = queries.size() - 1; i >= 0; --i) {
const int frequency{calculateFrequency(queries[i])};
const auto it = std::upper_bound(frequencies.begin(), frequencies.end(), frequency);
output[i] = std::distance(it, frequencies.end());
}
return output;
}
int calculateFrequency(std::string_view v) {
char c = v[0];
int frequency = 1;
for (int i = v.size() - 1; i > 0; --i) {
if (v[i] <= c) {
if (v[i] == c) {
++frequency;
} else {
c = v[i];
frequency = 1;
}
}
}
return frequency;
}
};