-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1170_num_smaller_by_frequency.cc
59 lines (53 loc) · 1.6 KB
/
1170_num_smaller_by_frequency.cc
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <string>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace::std;
class Solution {
public:
vector<int> numSmallerByFrequency(vector<string>& queries, vector<string>& words) {
vector<int> answers, nums_words, nums_queries;
for (const auto& s : words) {
int nums = 0;
char comp = 'z';
for (const auto& c : s) {
if (c < comp) {
nums = 1;
comp = c;
} else if (c == comp) {
++nums;
}
}
nums_words.push_back(nums);
}
for (const auto& s : queries) {
int nums = 0;
char comp = 'z';
for (const auto& c : s) {
if (c < comp) {
nums = 1;
comp = c;
} else if (c == comp) {
++nums;
}
}
nums_queries.push_back(nums);
}
sort(nums_words.begin(), nums_words.end());
for (int i = 0; i < nums_queries.size(); ++i) {
auto iter = upper_bound(nums_words.begin(), nums_words.end(), nums_queries[i]);
if (iter == nums_words.end())
answers.push_back(0);
else
answers.push_back(nums_words.end() - iter);
}
return answers;
}
};
int main()
{
vector<string> queries{"bbb", "cc"}, words{"a", "aa", "aaa", "aaaa"};
Solution solu;
solu.numSmallerByFrequency(queries, words);
return 0;
}