1170. 比较字符串最小字母出现频次

题目链接 1170. 比较字符串最小字母出现频次
思路 题意不易理解;排序+二分(upper_bound)
题解链接 Python简洁解法
关键点 预先处理words
时间复杂度 O((n+m)p)
空间复杂度 O(1)

代码实现:

class Solution:
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
nums = sorted(
s.count(min(s)) for s in words
)
n = len(nums)
def upper_bound(val):
left, right = -1, n
while left+1<right:
mid = (left+right) // 2
if nums[mid] > val:
right = mid
else:
left = mid
return right
return [
n-upper_bound(q.count(min(q))) for q in queries
]
Python-官方库
class Solution:
def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]:
nums = sorted(
s.count(min(s)) for s in words
)
n = len(nums)
return [
n-bisect_right(nums, q.count(min(q))) for q in queries
]
posted @   WrRan  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
点击右上角即可分享
微信分享提示