forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1170.js
30 lines (28 loc) · 761 Bytes
/
1170.js
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
var numSmallerByFrequency = function(queries, words) {
function f(str) {
var cnt = 0, minChar = '~';
for (let c of str) {
if (c < minChar) minChar = c, cnt = 1;
else if (c == minChar) ++cnt;
}
return cnt;
}
var res = [], w = [];
for (let word of words) {
w.push(f(word));
}
w = w.sort(function sortNumber(a,b) {
return a - b;
});
for (var i = 0; i < queries.length; ++i) {
var cnt = f(queries[i]), l = 0, r = w.length - 1;
while (l < r) {
var mid = l + r >> 1;
if (cnt < w[mid]) r = mid;
else l = mid + 1;
}
if (cnt >= w[l]) l++;
res[i] = w.length - l;
}
return res;
};