搜索推荐系统
Search Suggestions System
题目详情
问题:搜索推荐系统
考察:数组、字符串、二分查找
来源:DSA Prep / Citadel
链接:https://leetcode.com/problems/search-suggestions-system
Problem: Search Suggestions System
Patterns: Array, String, Binary Search
Recency: 2yr
Link: https://leetcode.com/problems/search-suggestions-system
Source: https://www.dsaprep.dev/blog/citadel-coding-interview-questions/
解析
思路:先把 products 排序。随着 searchWord 前缀增长,用二分找到前缀在排序数组中的下界,再向后收集最多三个仍匹配前缀的字符串。
复杂度:排序 O(n log n),每个前缀查询 O(log n + 3L),空间 O(1) 不计输出。