forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1170.go
38 lines (35 loc) · 807 Bytes
/
1170.go
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
func f(str string) int {
cnt, minChar := 0, '~'
for _, c := range str {
if c < minChar {
minChar, cnt = c, 1
} else if c == minChar {
cnt++
}
}
return cnt
}
func numSmallerByFrequency(queries []string, words []string) []int {
res := make([]int, len(queries))
w := make([]int, len(words))
for i, word := range words {
w[i] = f(word)
}
sort.Ints(w)
for i, v := range queries {
cnt, l, r := f(v), 0, len(w) - 1
for l < r {
mid := (l + r) >> 1
if cnt < w[mid] {
r = mid
} else {
l = mid + 1
}
}
if cnt >= w[l] {
l++
}
res[i] = len(w) - l
}
return res
}