LeetCode、1268. 搜索推荐系统【中等,前缀树+优先队列、排序+前缀匹配】

文章目录

前言

博主介绍:✌目前全网粉丝2W+,csdn博客专家、Java领域优质创作者,博客之星、阿里云平台优质作者、专注于Java后端技术领域。

涵盖技术内容:Java后端、算法、分布式微服务、中间件、前端、运维、ROS等。

博主所有博客文件目录索引:博客目录索引(持续更新)

视频平台:b站-Coder长路


LeetCode、1268. 搜索推荐系统【中等,前缀树+优先队列、排序+前缀匹配】

题目类型及分类

题目链接:LeetCode、1268. 搜索推荐系统

分类:02数据结构/树/字典树(前缀树)


思路

API调用(排序+前缀匹配)

复杂度分析:时间复杂度O(n.logn);空间复杂度O(n)

java 复制代码
class Solution {

    //prodcuts数量2万  单词1000
    public List<List<String>> suggestedProducts(String[] products, String searchWord) {
        //根据字典序排序
        Arrays.sort(products);
        //初始化结果集合
        List<List<String>> ans = new ArrayList<>();
        //遍历所有的搜索文字
        for (int i = 0; i < searchWord.length(); i ++) {
            String s = searchWord.substring(0, i + 1);
            //结果集合
            List<String> res = new ArrayList<>();
            //遍历所有products
            for (String product: products) {
                if (product.startsWith(s)) {
                    res.add(product);
                }
                //若是已经收集到3个
                if (res.size() == 3) {
                    break;
                }
            }
            ans.add(res);
        }
        return ans;
    }
}

前缀树+优先队列

使用大顶堆目的:每个单词只留下3个最小字典序的,因为一旦大顶堆中有>目标数量时,就会将最大的排除出去。

复杂度分析:时间复杂度O(n);空间复杂度O(n)

java 复制代码
class Solution {

    //每一个大顶堆中的数量为3
    private static final int size = 3;
    //根节点
    private TrieNode root = new TrieNode();

    //自定义Node节点
    class TrieNode {
        public static final int num = 26;
        TrieNode[] children;
        boolean isEnd;
        PriorityQueue<String> queue;

        public TrieNode() {
            this.children = new TrieNode[num];
            this.queue = new PriorityQueue<>((o1,o2)->o2.compareTo(o1));
        }
    }

    //插入一个产品名称到前缀树
    public void insert(String product) {
        //拿到当前的前缀树节点
        TrieNode node = this.root;
        //遍历整个名称字符
        for (char ch: product.toCharArray()) {
            int index = ch - 'a';
            if (node.children[index] == null) {
                node.children[index] = new TrieNode();
            }
            node = node.children[index];
            //当前节点要将单词添加进来
            node.queue.offer(product);
            if (node.queue.size() > size) {
                node.queue.poll();
            }
        }
        node.isEnd = true;
    }

    public List<List<String>> suggestedProducts(String[] products, String searchWord) {
        List<List<String>> ans = new ArrayList<>();
        //遍历所有的商品名称,依次添加到前缀树中
        for (String product: products) {
            insert(product);
        }
        //搜索所有的匹配结果
        TrieNode node = this.root;
        for (char ch: searchWord.toCharArray()) {
            int index = ch - 'a';
            //临时保存一个集合
            List<String> tmp = new ArrayList<>();            
            if (node == null) {
                ans.add(tmp);
                continue;
            }
            node = node.children[index];
            //节点不为空情况
            while (node != null && !node.queue.isEmpty()) {
                tmp.add(node.queue.poll());
            }
            //将整个集合翻转
            Collections.reverse(tmp);
            //添加到最终的结果集合中
            ans.add(tmp);
        }
        return ans;
    }

}

资料获取

大家点赞、收藏、关注、评论啦~

精彩专栏推荐订阅:在下方专栏👇🏻

更多博客与资料可查看👇🏻获取联系方式👇🏻,🍅文末获取开发资源及更多资源博客获取🍅


整理者:长路 时间:2024.2.13

相关推荐
_OP_CHEN6 小时前
【算法基础篇】(五十七)线性代数之矩阵乘法从入门到实战:手撕模板 + 真题详解
线性代数·算法·矩阵·蓝桥杯·c/c++·矩阵乘法·acm/icpc
天天爱吃肉82186 小时前
【跨界封神|周杰伦×王传福(陶晶莹主持):音乐创作与新能源NVH测试,底层逻辑竟完全同源!(新人必看入行指南)】
python·嵌入式硬件·算法·汽车
im_AMBER6 小时前
Leetcode 114 链表中的下一个更大节点 | 删除排序链表中的重复元素 II
算法·leetcode
xhbaitxl6 小时前
算法学习day38-动态规划
学习·算法·动态规划
多恩Stone6 小时前
【3D AICG 系列-6】OmniPart 训练流程梳理
人工智能·pytorch·算法·3d·aigc
历程里程碑6 小时前
普通数组----轮转数组
java·数据结构·c++·算法·spring·leetcode·eclipse
pp起床6 小时前
贪心算法 | part02
算法·leetcode·贪心算法
sin_hielo6 小时前
leetcode 1653
数据结构·算法·leetcode
2501_901147836 小时前
面试必看:优势洗牌
笔记·学习·算法·面试·职场和发展
YuTaoShao7 小时前
【LeetCode 每日一题】3634. 使数组平衡的最少移除数目——(解法二)排序 + 二分查找
数据结构·算法·leetcode