class Solution1(object):
def numSmallerByFrequency(self, queries, words):
"""
直接统计
"""
from collections import Counter
def helper(string):
string = sorted(list(Counter(string).items()), key=lambda x: x[0])
return string[0][1]
queries = list(map(helper, queries))
words = list(map(helper, words))
result = []
for q in queries:
result.append(0)
for w in words:
if w > q:
result[-1] += 1
return result
class Solution(object):
def numSmallerByFrequency(self, queries, words):
"""
排序后利用bitset 函数找到插入的位置即可
"""
from collections import Counter
import bisect
def helper(string):
string = sorted(list(Counter(string).items()), key=lambda x: x[0])
return string[0][1]
queries = list(map(helper, queries))
words = list(map(helper, words))
words.sort()
result = []
for q in queries:
index = len(words) - bisect.bisect(words, q)
result.append(index)
return result
class Solution(object):
def numSmallerByFrequency(self, queries, words):
"""
short is better
"""
import bisect
words = sorted(w.count(min(w)) for w in words)
result = [ (len(words)- bisect.bisect(words, q.count(min(q)))) for q in queries]
return result
LC.1160. Find Words That Can Be Formed by Characters
最新推荐文章于 2020-12-30 17:02:46 发布