forked from luliyucoordinate/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1170.go
44 lines (39 loc) · 1007 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
39
40
41
42
43
44
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.Sort(IntSlice(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
}
type IntSlice []int
func (s IntSlice) Len() int { return len(s) }
func (s IntSlice) Swap(i, j int){ s[i], s[j] = s[j], s[i] }
func (s IntSlice) Less(i, j int) bool { return s[i] < s[j] }